如何使用实体框架中的位置来计算关联实体

发布于 2024-10-17 22:25:24 字数 960 浏览 1 评论 0 原文

我有这个:

        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

I have this:

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

But I need something like this:

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

But post.Comments is a ICollection

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忆梦 2024-10-24 22:25:24

如何使用 Enumerable.Cast 像这样吗?

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

How about using Enumerable.Cast<T>() like this ?

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();

assuming post.Comments is of type Comment

故事和酒 2024-10-24 22:25:24

尝试:

var queryResult = (from post in context.Posts
                select new
                           {
                               post,
                               post.Author,
                               post.Tags,
                               post.Categories,
                               Count = context.Comments.Where(c => c.Post == post).Where(c => IsPublic == 1).Count()
                           }).ToList();

Try:

var queryResult = (from post in context.Posts
                select new
                           {
                               post,
                               post.Author,
                               post.Tags,
                               post.Categories,
                               Count = context.Comments.Where(c => c.Post == post).Where(c => IsPublic == 1).Count()
                           }).ToList();
时光暖心i 2024-10-24 22:25:24

这有效:

        var queryResult = (from post in posts
                           join comment in comments.Where(x=> x.IsPublic) on post.Id equals comment.Post.Id into g
                    select new
                               {
                                   post,
                                   post.Author,
                                   post.Tags,
                                   post.Categories,
                                   Count = g.Count()
                               })

但在所有解决方案中,我们都有这个问题 如何在实体框架中的同一查询中使用包含和匿名类型?

This works:

        var queryResult = (from post in posts
                           join comment in comments.Where(x=> x.IsPublic) on post.Id equals comment.Post.Id into g
                    select new
                               {
                                   post,
                                   post.Author,
                                   post.Tags,
                                   post.Categories,
                                   Count = g.Count()
                               })

But in all solutions we have this problem How to use Include and Anonymous Type in same query in Entity Framework?

美胚控场 2024-10-24 22:25:24

我按照 LukLed 的方式编写了它,但为了使其工作,我将使用 Post 实体的 PK:

var queryResult = (from post in context.Posts
                select new
                           {
                               post,
                               post.Author,
                               post.Tags,
                               post.Categories,
                               Count = context.Comments.Where(c => c.Post.Id == post.Id && c.IsPublic == 1).Count()
                           }).ToList();

或者如果通过关联暴露外键,则 Post.Id 可以写为 PostId,我相信这会更有效。

I'd written it as LukLed did but to make it work I'll use the PK of Post entity:

var queryResult = (from post in context.Posts
                select new
                           {
                               post,
                               post.Author,
                               post.Tags,
                               post.Categories,
                               Count = context.Comments.Where(c => c.Post.Id == post.Id && c.IsPublic == 1).Count()
                           }).ToList();

Or Post.Id could just be written as PostId if forign keys are exposed through the association which I believe it would be more efficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文