What is right way to query "Many-Many-Many" relationship with EF 4.1?
The Model as below:
public class User
{
public int Id
public virtual ICollection<Tag> FollowingTags {get;set;}
}
public class Tag
{
public int Id
public virtual ICollection<User> Followers {get;set;}
public virtual ICollection<Post> Posts {get;set;}
}
public class Post {
public int Id
public virtual ICollection<Tag> Tags {get;set;}
}
That means there are two many-to-many from User to Post, perhaps it can be called a M:M:M relationship.
Now, if I want to find all posts with tags that followed by a certain User. I wonder what is the best practice with EF 4.1?
If use ADO.NET, I think joining two joint tables is effective way, but joint tables are hidden in EF, then how to do it ? I know some solutions, but the performance 开发者_如何学JAVAis not good, because the generated SQL not good enough. so I ask for a good query to get good performance.
Thank you!
Try using the "include" modifier in your query like:
context ctx = new context() // context is the entity context in this case
var query = from p in ctx.posts.include("tags.users") where p.tags.Followers.ID = TargetUserID
This should cover it
(from p in db.Posts
from t in p.Tags
from f in t.Followers
where f.Id == id
select p).Distinct()
精彩评论