【发布时间】:2015-07-04 22:24:08
【问题描述】:
我正在为一个家庭项目使用实体框架。
例如,假设我有两个列表:A 和 B
我需要能够根据两者之间的共同属性匹配列表A和B中的项目(选择列表A和B中具有相同价值和相同城市的所有房屋)
一个 House 在两个列表中可以有两个共同的属性(例如 Value 和 City),但其他一些属性可能不同(例如 HouseId),因此比较对象不会返回正确的匹配项。
我可以做两个嵌套的 foreach,但由于 LINQ 查询是为了使用 EF 查询数据库,所以我需要使用 LINQ。
这是我到目前为止所做的,但匹配不正确:
public IEnumerable<ApplicationUser> GetMatchesForCurrentUser(ApplicationUser currentUser)
{
var prematches = (from user in _memberRepository.All()
where user.Zone == currentUser.Zone &&
user.Time == currentUser.Time &&
user.HouseOfUser.Any(g => currentUser.HouseOfUser.Any(x => x.CityId == g.CityId))
select user).ToList();
var matches = (from prematch in prematches
where prematch.HouseOfUser.Any(g => currentUser.HouseOfUser.Any(x => x.Value == g.Value))
select prematch).ToList();
return matches;
}
【问题讨论】:
标签: c# linq entity-framework