我有两个相同类类型的列表。该类的属性是:
public string Name { get; set; }
public string Severity { get; set; }
public DateTime DetectedMissingUTC { get; set; }
public DateTime DetectedMissingLocal { get; set; }
考虑列表 A 包含以下内容的情况:
[0] Item.Name = "Bob"
[1] Item.Name = "Rob"
列表 B 包含:
[0] Item.Name = "Bob"
[1] Item.Name = "Rob"
[2] Item.Name = "Robert"
我想用 [2] Item.Name = "Robert" 做点什么,因为它是一个新条目。我该如何编码?在乱码的伪代码术语中,我需要:
foreach (var ListB.item NOT IN ListA)
{
Do something with item
}
请您参考如下方法:
这应该给你你想要的:
foreach (var item in ListB.Where(x => !ListA.Any(a => a.Name == x.Name))
{
...
}
如果你想将所有只存在于ListB中的项目添加到listA,你可以这样做:
ListA.AddRange(ListB.Where(x => !ListA.Any(a => a.Name == x.Name));
