C# 如何";Include()。其中();在实体框架中?

C# 如何";Include()。其中();在实体框架中?,c#,entity-framework,linq,C#,Entity Framework,Linq,如何过滤EF Linq查询中的嵌套包含?例如,如果我有: var blogs = context.Blogs .Include(blog => blog.Posts) .ThenInclude(post => post.Author) .ThenInclude(author => author.Photo) .ToList(); 但我只想在某一天之后收到博客上的帖子,或是写着“马克”名字的作者 我希望能够以某种方式做到以下几点: var blogs = con

如何过滤EF Linq查询中的嵌套包含?例如,如果我有:

var blogs = context.Blogs
.Include(blog => blog.Posts)
    .ThenInclude(post => post.Author)
    .ThenInclude(author => author.Photo)
.ToList();
但我只想在某一天之后收到博客上的帖子,或是写着“马克”名字的作者

我希望能够以某种方式做到以下几点:

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

还是我错过了一些很简单的东西?提前谢谢你的建议

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();
EF将仅在调用
ToList()
时创建实际查询,从而构造有效的查询


请参见是否禁用了
懒散加载
?我很确定你可以做一些像
.Include(blog=>blog.Posts.Where(yourchation))
谢谢Alex-是的,这是重复的-非常抱歉!这不会编译,因为Author是Post的属性,因此需要使用thenclude()。在您的示例中,编译器希望Author是Blog的属性。