开发者

linq to entities comparing object lists

开发者 https://www.devze.com 2023-03-29 16:35 出处:网络
I have a member object that has a one to many relationship with a phones object. i want to get a list of members that have the same name and have a phone no that also matches.

I have a member object that has a one to many relationship with a phones object. i want to get a list of members that have the same name and have a phone no that also matches.

I can match the records but the phone numbers are not being included because of the join. Any ideas how i might get the list of phone numbers returned?

Code so far:

            var membersA = dc.Members.Include("Phones");
            var membersB = dc.Members.Include("Phones");

            var matchingMembers = from ma in membersA
                                  join mb in membersB
                            开发者_Go百科      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;


can't you do something like:

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 };

Update: It appear to be some limitation with linq-to-entities and include. The code above is updated so it should work so it selects an anonymous object. I haven't tried it and don't know what the SQL will look like, but it could be one solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消