如何过滤EF Linq查询中的嵌套包含?例如,如果我有:
var blogs = context.Blogs
.Include(blog => blog.Posts)
.ThenInclude(post => post.Author)
.ThenInclude(author => author.Photo)
.ToList();
但我只希望在特定日期之后获得带帖子的博客,或者通过名称' Mark'来获取作者。
我希望能够以某种方式做到以下几点:
var blogs = context.Blogs
.Include(blog => blog.Posts).Where(... blog.Posts.Date>X.... )
.ThenInclude(post => post.Author).Where(... post.Author.Name=='Mark' ...)
.ThenInclude(author => author.Photo)
.ToList();
或者我错过了一些非常简单的东西?提前感谢您的建议!
答案 0 :(得分:-2)
EF允许您提出要求而无需额外工作
var blogs = context.Blogs
.Include(blog => blog.Posts).Where(b => b.Posts.Date>X)
.Include(post => post.Author).Where(p => p.Author.Name=='Mark')
.Include(author => author.Photo)
.ToList();
只有在调用ToList()
时,EF才会创建实际查询,从而构建有效的查询。
请参阅https://msdn.microsoft.com/en-us/library/bb738633(v=vs.110).aspx