条件包括:在LINQ to实体 [英] condition in include in linq to entities

查看:174
本文介绍了条件包括:在LINQ to实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

文章

PostXTags

PostTags

有帖子,PostXTags之间PostTags之间,PostXTags

1-n的关系

我需要把所有的帖子与他们的标签。

有一个在PostTags叫类型的字段,我想有它的过滤器。在includ每一个条件会遇到这样的错误:

包括路径EX pression必须引用的类型定义的导航属性。借鉴导航属性和集合导航属性选择运营商虚线路径。 参数名称:路径

 公共的IQueryable<邮政> GetPosts()
    {
        从对在_db.Posts返回
               选择磷;
    }

    变种帖= GetPosts()中包括:(p值=> p.PostXTags.Select(PXT => pxt.PostTag)。凡(PT => pt.Type == 2))。了ToList();
 

解决方案

我觉得你要实现的目标是什么

  VAR帖= _db.Posts.Include(PostXTags.PostTag)。
    其中,(p值=> p.PostXTags.Any(PXT => pxt.PostTags.Any(PT => pt.Type == 2)));
 

I've got this really basic table structure:

Posts

PostXTags

PostTags

There is a 1-n relation between Posts, PostXTags and between PostTags, PostXTags

I need to get all Posts with their tags.

There is a field called type in PostTags and I want to have a filter on it. Every condition in includ encounter this error:

The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties. Parameter name: path

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

解决方案

I think what you are trying to achieve is

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

这篇关于条件包括:在LINQ to实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆