且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

排除具有不同对象数据类型LINQ的另一个列表中的项吗?

更新时间:2023-01-16 19:46:01

与其创建新对象,不如在linq查询中检查属性

Instead of creating new objects, how about checking the properties as part of the linq query

List<Human> humans = _unitOfWork.GetHumans();
List<AnotherHuman> anotherHumans = _unitofWork.GetAnotherHumans();

// Get all anotherHumans where the record does not exist in humans
var result = anotherHumans
               .Where(ah => !humans.Any(h => h.LastName == ah.LastName
                               && h.Name == ah.Name
                               && h.Birthday == ah.Birthday
                               && (!h.PersonalId.HasValue || h.PersonalId == ah.PersonalId)))
               .ToList();