条件包含在linq中的实体

Gho*_*ngi 4 .net linq linq-to-entities entity-framework

我有这个非常基本的表结构:

帖子

PostXTags

PostTags

Posts,PostXTags和PostTags,PostXTags之间存在1-n关系

我需要使用他们的标签获取所有帖子.

PostTags中有一个名为type的字段,我希望在其上有一个过滤器.包含中的每个条件都会遇到此错误:

Include路径表达式必须引用在类型上定义的导航属性.使用虚线路径作为参考导航属性,使用Select运算符作为集合导航属性.参数名称:路径

    Public IQueryable<Post> GetPosts()
    {
        return from p in _db.Posts
               select p;
    }

    var posts = GetPosts().Include(p => p.PostXTags.Select(pxt => pxt.PostTag).Where(pt=>pt.Type == 2)).ToList();
Run Code Online (Sandbox Code Playgroud)

Phi*_*hil 6

我想你想要实现的目标是什么

var posts = _db.Posts.Include("PostXTags.PostTag").
    Where(p => p.PostXTags.Any( pxt => pxt.PostTags.Any( pt => pt.Type == 2 ) ));
Run Code Online (Sandbox Code Playgroud)