Reputation: 605
I have two lists with two fields. I don't know before hand which one is larger.
I want to achieve this:
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
One the left you can see newList on the right the oldList. If I compare oldList against newList I want to see every possible row and difference in Quantity between the two lists.
I wrote this, which would give me Fruit that's not contained in both lists (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());
But I'm lost on how to combine it with Qty as well.
Upvotes: 0
Views: 1333
Reputation: 174299
I don't see a way with LINQ that performs well and is also readable. I would prefer a mix of LINQ and loops:
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 });
}
The class FruitSummary
looks like this:
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; }
}
Upvotes: 3
Reputation: 8894
Something like this?
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) });
When I run foreach(var d in difference) Console.WriteLine(d);
, I get:
{ 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 }
Upvotes: 0
Reputation: 43876
I assume you have a Fruit
class like this:
public class Fruit
{
public string Name {get; set;}
public int Quantity {get; set;}
}
and you have an old and a new list like these
List<Fruit> oldList = ...
List<Fruit> newList = ...
Then this LINQ monstrum does the job, though it may not be the most performant solution (but how many kinds of fruit are there?):
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}));
You can output the results like this:
Console.WriteLine(string.Join(Environment.NewLine, result.Select(r => $"{r.Name} {r.Diff}")));
Note that I come up with this because I like linq "one liners", but using a foreach
seems more readable and probably even faster than this query.
Upvotes: 2