使用linq从两个对象列表创建一个列表

我有以下情况


class Person

{

    string Name;

    int Value;

    int Change;

}


List<Person> list1;

List<Person> list2;

我需要将2个列表合并为一个新列表List<Person> ,以防合并记录具有该名称的同一个人,即list2中人员的值,更改将是list2的值-list1的值。如果没有重复,则更改为0


翻阅古今
浏览 453回答 3
3回答

跃然一笑

这是林克var mergedList = list1.Union(list2).ToList();这是Normaly(AddRange)var mergedList=new List<Person>();mergeList.AddRange(list1);mergeList.AddRange(list2);这是常态(Foreach)var mergedList=new List<Person>();foreach(var item in list1){&nbsp; &nbsp; mergedList.Add(item);}foreach(var item in list2){&nbsp; &nbsp; &nbsp;mergedList.Add(item);}这是常态(Foreach-Dublice)var mergedList=new List<Person>();foreach(var item in list1){&nbsp; &nbsp; mergedList.Add(item);}foreach(var item in list2){&nbsp; &nbsp;if(!mergedList.Contains(item))&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp;mergedList.Add(item);&nbsp; &nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP