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.

5
  • What are you going to do with Price value? Rewrite, sum, use the latest, etc,? And you should pass custom IEqualityComparer for Distinct method Commented Jan 14, 2020 at 20:13
  • Does this answer your question? How to merge 2 List<T> and removing duplicate values from it in C# Commented Jan 14, 2020 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 Items are the same. Or use the overrides that take a IEqualityComparer. Commented Jan 14, 2020 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. Commented Jan 14, 2020 at 20:20
  • @PavelAnikhouski Add the prices together for same Id's. in var combined Commented Jan 14, 2020 at 20:26

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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) });

1 Comment

@squareskittles Added comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.