How to perform a many-to-many Linq query with Include in the EF.

Posted by despart on Stack Overflow See other posts from Stack Overflow or by despart
Published on 2010-04-12T11:35:37Z Indexed on 2010/04/12 12:33 UTC
Read the original article Hit count: 322

Filed under:
|
|

Hi, I don't know how to perform this query using Linq and the EF.

Imagine I have three tables A, B and C.

A and B have a many-to-many relationship. B and C have a 1-to-many relationship.

I want to obtain records from B including C but filtering from A's Id. I can get easily the records from B:

var b = Context.A.Where(x => x.Id.Equals(aId)).SelectMany(x => x.B);

but when I try to include C I don't know how to do it:

//This doesn't work    
var b = Context.A.Where(x => x.Id.Equals(aId)).SelectMany(x => x.B.Include("C"));

Also I've tried this with no luck (it is equivalent to the above):

//Not working
var b = (from a in Context.A.Where(x => x.Id.Equals(aId))
         from b in a.B.Include("C")
         select b);

Thanks for your help.

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about c#