【发布时间】:2019-06-29 00:33:59
【问题描述】:
我有两个包含两个字段的列表。我事先不知道哪个更大。
我想实现这个:
newList oldList
Fruit Qty Diff Fruit Qty
--------------------------------------------
apple 3 +1 apple 2
-2 pear 2
peach 4 +3 peach 1
melon 5 0 melon 5
coconut 2 +2
mango 4 0 mango 4
banana 2 -1 banana 3
lychi 1 +1
-3 pineapple 3
左边一个你可以在右边看到newList oldList。如果我将 oldList 与 newList 进行比较,我想查看每个可能的行以及两个列表之间的 Quantity 差异。
我写了这个,它会给我两个列表中都不包含的水果(xor)
var difference = newList.Select(x => x.Fruit)
.Except(oldList.Select(x => x.Fruit))
.ToList()
.Union(oldList.Select(x => x.Fruit)
.Except(newList.Select(x => x.Fruit))
.ToList());
但我也不知道如何将它与 Qty 结合起来。
【问题讨论】:
-
这似乎是使用普通的
foreach然后在该循环中使用简单的LINQ。这些真的是清单吗?还是它们实际上是字典? -
你的
Fruit是一个班级吗?如果是这样,请将其添加到您的问题中。 -
这些列表是来自数据库的 linq 查询的结果,我只取了两列,水果和数量。所以 Fruit 是一个字段。