Merge and Update Two Lists in C#

后端 未结 6 1222
一生所求
一生所求 2021-01-18 07:12

I have two List objects:

For example:

List 1:
ID, Value where Id is populated and value is blank and it contains s

6条回答
  •  粉色の甜心
    2021-01-18 08:13

    Dictionary List1 = new Dictionary();
    List1.Add(1,"");
    List1.Add(2,"");
    List1.Add(3,"");
    List1.Add(4,"");
    List1.Add(5,"");
    List1.Add(6,"");
    
    Dictionary List2 = new Dictionary();
    List2.Add(2, "two");
    List2.Add(4, "four");
    List2.Add(6, "six");
    
    var Result = List1.Select(x => new KeyValuePair(x.Key, List2.ContainsKey(x.Key) ? List2[x.Key] : x.Value)).ToList();
    

提交回复
热议问题