LINQ加入到一个一到多用。凡()不工作? [英] LINQ Include on a one-to-many with .Where() not working?
问题描述
我想查询用户,包括每个用户的兴趣,但只有在有兴趣满足一定的条件:
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:
包含路径前pression必须指向一个导航属性
的类型来定义。借鉴导航路径点
属性和集合导航选择运营商
属性。
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.
推荐答案
要只包括一些利益,你将无法使用包含
方法,如它不支持这一点。你需要手动加入兴趣
与用户
:
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};
这篇关于LINQ加入到一个一到多用。凡()不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!