如何使用包含和匿名实体框架中键入相同的查询? [英] How to use Include and Anonymous Type in same query in Entity Framework?
问题描述
在如何计算关联实体使用Where In Entity Framework 我得到这个查询
但是当我访问queryResult [0] .post.Category或queryResult [0] .post.Tags它总是空的,因为我不使用Include。
包括不使用Projection,如微软在最后一个项目说: http://msdn.microsoft.com/en-us/library/bb896317.aspx
var queryResult =(来自帖子中的帖子
在帖子中的comments.Where(x => x.IsPublic)中加入注释。 Id等于comment.Post.Id into g
select new
{
post,
post.Author,
post.Tags,
post.Categories,
Count = g.Count()
})
在同一个查询中的计数,以及包括与标签和类别的关系?
为什么EF关系修复不适用于此?
var posts =
from context.Posts.Include(p => p.Author).Include(p => p.Tags).Include(p => p.Categories)
其中post.Comments.Any(c => c.IsPublic)
select邮政
var counts =
从post.Posts中的post.Posts
其中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(post中的var项)
{
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
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?
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
这篇关于如何使用包含和匿名实体框架中键入相同的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!