Exclude items of one list in another with different object data types, LINQ?

后端 未结 3 1042
后悔当初
后悔当初 2020-12-30 05:35

I have two lists filled with their own data. lets say there are two models Human and AnotherHuman. Each model contains different fields, however th

3条回答
  •  感动是毒
    2020-12-30 06:09

    var nonIdItems = anotherHumans
       .Where(ah => !ah.PersonalID.HasValue)
       .Join(humans, 
             ah => new{ah.LastName, 
               ah.FirstName, 
               ah.Birthday}, 
             h => new{h.LastName, 
               h.FirstName, 
               h.Birthday}, 
             (ah,h) => ah);
    var idItems = anotherHumans
       .Where(ah => ah.PersonalID.HasValue)
       .Join(humans, 
             ah => ah.PersonalID
             h => h.PersonalID, 
             (ah,h) => ah);
    var allAnotherHumansWithMatchingHumans = nonIdItems.Concat(idItems);
    var allAnotherHumansWithoutMatchingHumans = 
          anotherHumans.Except(allAnotherHumansWithMatchingHumans);
    

提交回复
热议问题