[英]Difference between two lists with linq
我有兩個帶有兩個字段的列表。 我不知道哪一個更大。
我想實現這一目標:
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
左邊的一個您可以在oldList的右側看到newList。 如果將oldList與newList進行比較,我想查看每個可能的行以及兩個列表之間的數量差異。
我寫了這個,這會給我兩個列表中未包含的水果(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相結合。
我看不到使用LINQ的方法性能良好且可讀性強的方法。 我希望混合使用LINQ和循環:
var oldGrouped = oldList.ToDictionary(x => x.Fruit, x => x.Qty);
var newGrouped = newList.ToDictionary(x => x.Fruit, x => x.Qty);
var result = new List<FruitSummary>();
foreach(var oldItem in oldGrouped)
{
var summary = new FruitSummary { OldFruit = oldItem.Key, OldQty = oldItem.Value };
if(newGrouped.TryGetValue(oldItem.Key, out int newQuantity) && newQuantity != 0)
{
summary.NewFruit = oldItem.Key;
summary.NewQty = newQuantity;
}
summary.Diff = oldItem.Value - newQuantity;
newGrouped.Remove(oldItem.Key);
result.Add(summary);
}
foreach(var newItem in newGrouped)
{
result.Add(new FruitSummary { Diff = -newItem.Value,
NewFruit = newItem.Key,
NewQty = newItem.Value });
}
類FruitSummary
看起來像這樣:
public class FruitSummary
{
public string OldFruit { get; set; }
public string NewFruit { get; set; }
public int OldQty { get; set; }
public int NewQty { get; set; }
public int Diff { get; set; }
}
我假設您有一個Fruit
類,如下所示:
public class Fruit
{
public string Name {get; set;}
public int Quantity {get; set;}
}
而且您有一個這樣的舊列表和新列表
List<Fruit> oldList = ...
List<Fruit> newList = ...
然后,這個LINQ monstrum就可以了,盡管它可能不是最高效的解決方案(但是有多少種水果?):
var result =
oldList.Join(newList,
oldFruit => oldFruit.Name,
newFruit => newFruit.Name,
(oldFruit, newFruit) => new {
Name = oldFruit.Name,
Diff = oldFruit.Quantity - newFruit.Quantity}).
Concat(oldList.
Where(oldFruit => newList.All(newFruit => newFruit.Name != oldFruit.Name)).
Select(oldFruit => new {
Name = oldFruit.Name,
Diff = -oldFruit.Quantity})).
Concat(newList.Where(newFruit => oldList.All(oldFruit => oldFruit.Name != newFruit.Name)).
Select(newFruit => new {
Name = newFruit.Name,
Diff = newFruit.Quantity}));
您可以輸出如下結果:
Console.WriteLine(string.Join(Environment.NewLine, result.Select(r => $"{r.Name} {r.Diff}")));
請注意,我之所以提出這個建議是因為我喜歡linq“一個內襯”,但使用foreach
似乎比此查詢更具可讀性,甚至可能更快。
像這樣嗎
var difference = newList
.Concat(oldList.Select(x => new { x.Fruit, Qty = -x.Qty }))
.GroupBy(x => x.Fruit)
.Select(g => new {Fruit = g.Key, Qty = g.Sum(x => x.Qty) });
當我運行foreach(var d in difference) Console.WriteLine(d);
,我得到:
{ Fruit = apple, Qty = 1 }
{ Fruit = peach, Qty = 3 }
{ Fruit = melon, Qty = 0 }
{ Fruit = coconut, Qty = 2 }
{ Fruit = mango, Qty = 0 }
{ Fruit = banana, Qty = -1 }
{ Fruit = lychi, Qty = 1 }
{ Fruit = pear, Qty = -2 }
{ Fruit = pineapple, Qty = -3 }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.