Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to query Users including each user's Interests, but only where an Interest meets certain criteria:

  return db.Users.Include(u => u.Interests.Where(s => s.TenantId == tenantId))

But I get an 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.

I played with the idea of pushing the .Where outside, but haven't been able to get it working.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
445 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try this:

return db.Users.Include("Interests").Where(u => u.Interests.Any(i => i.TenantId == tenantId));

This causes the users to be loaded, but only where the tenantId matches. The Interests related entities will be eager loaded for those users when the query executes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...