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.Interest
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.
To include only some of the interests you won't be able to use the Include
method, as it doesn't support this. You'll need to manually join the Interests
with the Users
:
var query = from user in db.Users
join interest in db.Interests.Where(s => s.TenantId == tenantId)
on user.InterestId equals interest.Id //todo: will need to be updated
into interests;
select new { user, interests};