C# 给定两个对象列表,如何比较C中的新KV?
我使用except查找修改过的对象,但是except使用一个比较器,只返回差异 返回此类结果的最佳算法是什么C# 给定两个对象列表,如何比较C中的新KV?,c#,algorithm,linq,set-difference,C#,Algorithm,Linq,Set Difference,我使用except查找修改过的对象,但是except使用一个比较器,只返回差异 返回此类结果的最佳算法是什么 谢谢。据我所知,您有两个这样的KeyValuePair: List<KeyValuePair<string, string>> list1 = new List<KeyValuePair<string, string>>(); List<KeyValuePair<string, string>> list2 = ne
谢谢。据我所知,您有两个这样的KeyValuePair:
List<KeyValuePair<string, string>> list1 = new List<KeyValuePair<string, string>>();
List<KeyValuePair<string, string>> list2 = new List<KeyValuePair<string, string>>();
或者,作为第二个选项,您可以迭代第二个列表并在第一个列表中获得匹配的元素:
var result = list2.Where(x => list1.Any(y => y.Key == x.Key && y.Value != x.Value))
.Select(x => new
{
NewKVP = new KeyValuePair<string, string>(x.Key, x.Value),
OldKVP = new KeyValuePair<string, string>(x.Key, list1.Single(z => z.Key == x.Key).Value)
})
.ToList();
但是,我更喜欢Join。据我所知,您有两个这样的KeyValuePair:
List<KeyValuePair<string, string>> list1 = new List<KeyValuePair<string, string>>();
List<KeyValuePair<string, string>> list2 = new List<KeyValuePair<string, string>>();
或者,作为第二个选项,您可以迭代第二个列表并在第一个列表中获得匹配的元素:
var result = list2.Where(x => list1.Any(y => y.Key == x.Key && y.Value != x.Value))
.Select(x => new
{
NewKVP = new KeyValuePair<string, string>(x.Key, x.Value),
OldKVP = new KeyValuePair<string, string>(x.Key, list1.Single(z => z.Key == x.Key).Value)
})
.ToList();
但是,我更喜欢加入