0

I have a object list like this:

using System;
using System.Collections.Generic;
using System.Linq;

public class Item
{
    public int Id;
    public int Price;
}

public class Program
{
    public static void Main()
    {
        List<Item> food = new List<Item> {
            new Item { Id = 1, Price = 1},
            new Item { Id = 2, Price = 3},
            new Item { Id = 4, Price = 9}
        };

        List<Item> drinks = new List<Item> {
            new Item { Id = 1, Price = 1},
            new Item { Id = 2, Price = 2},
            new Item { Id = 3, Price = 0},
            new Item { Id = 4, Price = 1},
            new Item { Id = 6, Price = 1}
        };

        List<Item> magazines = new List<Item> {
            new Item { Id = 3, Price = 1},
            new Item { Id = 5, Price = 2},
        };

        var combined = food.Union(drinks).Union(magazines).Distinct().ToList();
    }
}

What I want to do is, add all the prices into one list. Without any duplicates (Id). My goal is to have the total sum of the prices. So basically add all prices for the same ID together.

So the combined list should look like this:

List<Item> combined = new List<Item> {
            new Item { Id = 1, Price = 2},
            new Item { Id = 2, Price = 5},
            new Item { Id = 3, Price = 1},
            new Item { Id = 4, Price = 10},
            new Item { Id = 5, Price = 2},
            new Item { Id = 6, Price = 1}
        };

Preferably using LINQ.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • What are you going to do with `Price` value? Rewrite, sum, use the latest, etc,? And you should pass custom `IEqualityComparer` for `Distinct` method – Pavel Anikhouski Jan 14 '20 at 20:13
  • Does this answer your question? [How to merge 2 List and removing duplicate values from it in C#](https://stackoverflow.com/questions/4031262/how-to-merge-2-listt-and-removing-duplicate-values-from-it-in-c-sharp) – Volkan Albayrak Jan 14 '20 at 20:15
  • 1
    Do you mean duplicate by id? Right now you're custom class does reference equality so that's what `Distinct` and `Union` will use to determine uniqueness. You'd need to override `Equals` and `GetHashCode` to tell it how to determine if two `Item`s are the same. Or use the overrides that take a `IEqualityComparer`. – juharr Jan 14 '20 at 20:15
  • @VolkanAlbayrak No. I don't want to combine the list. What I want to do is add the prices of those tree lists together. – Franz Peter Tebartz van Elst Jan 14 '20 at 20:20
  • @PavelAnikhouski Add the prices together for same Id's. in `var combined` – Franz Peter Tebartz van Elst Jan 14 '20 at 20:26

2 Answers2

2

If you need to get a sum of prices for concatenated List<Item>, you should use GroupBy method to group the items by Id and then Sum of prices for every group

var combined = food.Concat(drinks).Concat(magazines)
    .GroupBy(i => i.Id, i => i.Price, (i, prices) => new Item { Id = i, Price = prices.Sum() })
    .OrderBy(i => i.Id).ToList();

You can also add OrderBy to sort the results by Id property, if it's important

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
1
var x =
    // First, combine all lists
    food.Concat(drinks).Concat(magazines)
    // Group combined Items by Id
    .GroupBy(item => item.Id)
    // From all groups create final Items with Id and summed Price
    .Select(g => new Item { Id = g.Key, Price = g.Sum(item => item.Price) });
JohnyL
  • 6,894
  • 3
  • 22
  • 41