gpt4 book ai didi

c# - 如何使用 Entity Framework 中的 Where 计算关联实体

转载 作者:太空宇宙 更新时间:2023-11-03 16:48:28 25 4
gpt4 key购买 nike

我有这个:

        var queryResult = (from post in posts
select new
{
post,
post.Author,
post.Tags,
post.Categories,
Count = post.Comments.Count()
}).ToList();

但是我需要这样的东西:

        var queryResult = (from post in posts
select new
{
post,
post.Author,
post.Tags,
post.Categories,
Count = post.Comments.Where(x=>x.IsPublic).Count()
}).ToList();

但是 post.Comments 是一个 ICollection

最佳答案

如何使用 Enumerable.Cast<T>() 像这样?

var queryResult = (from post in posts
select new
{
post,
post.Author,
post.Tags,
post.Categories,
Count = post.Comments.Cast<Comment>()
.Where(x=>x.IsPublic).Count()
}).ToList();

假设post.Comments类型为 Comment

关于c# - 如何使用 Entity Framework 中的 Where 计算关联实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5024199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com