我有一个与phone对象有一对多关系的成员对象。我想获得一个名字相同的成员列表,并且电话号码也不匹配。
我可以匹配记录但是因为加入而未包含电话号码。我有什么想法可以获得电话号码列表吗?
到目前为止代码:
var membersA = dc.Members.Include("Phones");
var membersB = dc.Members.Include("Phones");
var matchingMembers = from ma in membersA
join mb in membersB
on new { ma.Forename, ma.Surname } equals new { mb.Forename, mb.Surname }
where ma.MemberID < mb.MemberID
select new { ma, mb };
if (SearchByPhone)
matchingMembers = from mm in matchingMembers
where mm.ma.Phones.Any(phoneA => mm.mb.Phones.Any(phoneB => phoneB.PhoneNumber == phoneA.PhoneNumber))
select mm;
答案 0 :(得分:0)
var membersA = dc.Members.Include("Phones");
var membersB = dc.Members.Include("Phones");
var matchingMembers = from ma in membersA
join mb in membersB
on new { ma.Forename, ma.Surname } equals new { mb.Forename, mb.Surname }
where ma.MemberID < mb.MemberID and ma.Phones.Any(pa => mb.Any.Phones(pb => pb.PhoneNumber == pa.PhoneNumber)
select new { Member = ma, Phones = ma.Phones };
更新:linq-to-entities和include似乎有些限制。上面的代码已更新,因此它应该工作,因此它选择一个匿名对象。我没有尝试过,也不知道SQL会是什么样子,但它可能是一个解决方案。