如何在实体框架中的同一查询中使用包含和匿名类型?

发布于 2024-10-17 23:25:31 字数 1029 浏览 1 评论 0原文

如何使用Where计算关联实体在实体框架中我得到这个查询

但是当我访问queryResult[0].post.Category或queryResult[0].post.Tags时它总是空的,因为我没有使用Inclusion。

包括不与投影一起使用,正如微软在此处的最后一项所说:http:// msdn.microsoft.com/en-us/library/bb896317.aspx

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

如何在同一查询中获取计数,并包含与标签和类别的关系?

为什么 EF 关系修复在这里不起作用?

In How To Count Associated Entities using Where In Entity Framework I get this query

But when I access queryResult[0].post.Category or queryResult[0].post.Tags it´s always empty, because I am not using Include.

Include dont work with Projection, as microsoft say at last item here: http://msdn.microsoft.com/en-us/library/bb896317.aspx

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

How I can get the Count in the same query, and Include relationship to Tags and Categories?

Why EF relationship fix-up dont work here?

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

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

发布评论

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

评论(1

街角迷惘 2024-10-24 23:25:31

这可以通过 2 个查询来完成:

var posts =
  from post in context.Posts.Include(p => p.Author).Include(p => p.Tags).Include(p => p.Categories)
  where post.Comments.Any(c => c.IsPublic)
  select post;
var counts =
  from post in context.Posts
  where post.Comments.Any(c => c.IsPublic)
  select new { PostId = post.Id, Count = post.Comments.Count() };
var countDictionary = counts.ToDictionary(e => e.PostId, e => e.Count);

foreach (var item in posts)
{
  System.Console.WriteLine("PostId {0}, TagCount {1}, PublicCommentCount {2}", item.Id, item.Tags.Count, countDictionary[item.Id]);
}

作者:Diego Vega:http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/c0bae1c1-08b2-44cb-ac29-68a6518d87bd

This can be done with 2 queries:

var posts =
  from post in context.Posts.Include(p => p.Author).Include(p => p.Tags).Include(p => p.Categories)
  where post.Comments.Any(c => c.IsPublic)
  select post;
var counts =
  from post in context.Posts
  where post.Comments.Any(c => c.IsPublic)
  select new { PostId = post.Id, Count = post.Comments.Count() };
var countDictionary = counts.ToDictionary(e => e.PostId, e => e.Count);

foreach (var item in posts)
{
  System.Console.WriteLine("PostId {0}, TagCount {1}, PublicCommentCount {2}", item.Id, item.Tags.Count, countDictionary[item.Id]);
}

By Diego Vega: http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/c0bae1c1-08b2-44cb-ac29-68a6518d87bd

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