【发布时间】:2017-02-27 18:19:09
【问题描述】:
我有两个列表 List<T> existingValues 和 List<T> newValues
newvalues 中的 foreach 项目我检查它是否存在于existingValues 中(我根据主键值检查值是否存在),如果它不存在,我会将它添加到existingValues。
现在如果 existingValues 中已经存在值,我想检查是否有任何列值不同,如果它们不同,我想更新它们,我该怎么做?
foreach(var item in newvalues)
{
if(!existingValues.Any(x => x.PrimaryKeyVal == item.PrimaryKeyVal))
{
newValues.Add(item);
}
if (existingValues.Any(x => x.PrimaryKeyVal == item.PrimaryKeyVal))
{
//do something here to check if both rows are same, if not update the row with new values
}
}
【问题讨论】:
-
你不能只将所有属性从一个设置到另一个吗?如果他们不同,他们会改变,如果不是,他们不会。此外,您可能应该使用字典而不是任何字典,它会快得多。
-
使用
intersect方法:stackoverflow.com/questions/5065593/… -
@Mhd 如果他要走那条路,您可能想使用
Except,因为他想更改不同的值。 -
@PeterDuniho 这根本不是那个问题的重复......