【发布时间】:2013-10-23 10:59:20
【问题描述】:
我有两个列表,里面填满了他们自己的数据。
假设有两个模型Human 和AnotherHuman。每个模型包含不同的字段,但是它们有一些共同的字段,例如LastName, FirstName, Birthday, PersonalID。
List<Human> humans = _unitOfWork.GetHumans();
List<AnotherHuman> anotherHumans = _unitofWork.GetAnotherHumans();
我想从列表anotherHumans 中排除项目,其中LastName, FirstName, Birthday 都等于列表humans 中任何项目的相应字段。
但是,如果anotherHumans 列表中的任何项目具有PersonalID 并且列表humans 中的项目具有相同的PersonalID,那么仅通过此PersonalID 将Human 与AnotherHuman 进行比较就足够了,否则LastName, FirstName and Birthday。
我尝试创建新的重复列表并将其从anotherHumans 中排除:
List<AnotherHuman> duplicates = new List<AnotherHuman>();
foreach(Human human in humans)
{
AnotherHuman newAnotherHuman = new AnotherHuman();
newAnotherHuman.LastName = human.LastName;
newAnotherHuman.Name= human.Name;
newAnotherHuman.Birthday= human.Birthday;
duplicates.Add(human)
}
anotherHumans = anotherHumans.Except(duplicates).ToList();
但是我如何比较两个列表中的PersonalID(如果它存在)(它可以为空)。有什么方法可以摆脱创建 AnotherHuman 的新实例和重复列表并仅使用 LINQ 的方法?
提前致谢!
【问题讨论】:
标签: c# asp.net-mvc linq list