基本上,我有两个像下面这样的大列表:
public class Items
{
public string ItemID { get; set; }
}
var oldList = new List<Items>(); // oldList
var newList = new List<Items>(); // new list
这两个列表都非常大,如果它们都很大(超过30秒),简单的双重foreach将不足以满足执行时间。
在我之前在stackoverflow上提出的问题中,有人回答了如何比较这两个相同列表并找到哪些项目具有不同的QuantitySold参数,然后将其存储在第三个名为“DifferentQuantityItems”的列表中,如下所示:
var differentQuantityItems =
(from newItem in newList
join oldItem in oldList on newItem.ItemID equals oldItem.ItemID
where newItem.QuantitySold != oldItem.QuantitySold
select newItem).ToList();
现在我想从这两个列表中得到以下内容:
- A list of items that are present in newList, but not in oldList
- A list of items that are present in oldList, but not in newList
我该如何实现这个?有人可以帮助我吗?
注:我会通过属性“ItemID”来“知道”其中一个列表中是否缺少任一项目...
LINQ
的intersection
和union
。https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.intersect?view=netframework-4.7.2 - AarifEquals
方法。 - Aarif