【问题标题】:condition in include in linq to entities包含在 linq 中的条件到实体
【发布时间】:2012-03-26 06:07:04
【问题描述】:

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

帖子

PostXTags

帖子标签

Posts、PostXTags 之间以及 PostTags、PostXTags 之间存在 1-n 关系

我需要获取所有带有标签的帖子。

PostTags 中有一个名为 type 的字段,我想对其进行过滤。包括的每个条件都会遇到此错误:

包含路径表达式必须引用在类型上定义的导航属性。对引用导航属性使用虚线路径,对集合导航属性使用 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();

【问题讨论】:

  • 出了什么问题?例外?错误?结果不正确?

标签: .net linq entity-framework linq-to-entities


【解决方案1】:

我认为你想要实现的是

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

【讨论】:

    猜你喜欢
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多