Ghooti Farangi
Ghooti Farangi

Reputation: 20156

condition in include in linq to entities

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

Upvotes: 4

Views: 4348

Answers (1)

Phil
Phil

Reputation: 42991

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

Upvotes: 6

Related Questions