s1cart3r
s1cart3r

Reputation: 354

Using linq to group by and merge property values of duplicates

Given a list of objects how can I group by and merge using Linq in C#

public class Program
{
    public static void Main()
    {
        List<ItermsPerPerson> authors = new List<ItermsPerPerson>()
        {
            new ItermsPerPerson("Joe", "Pens"),
            new ItermsPerPerson("Jim", "Pencils"),
            new ItermsPerPerson("Joe", "Cycle"),
        };

        var list = authors.GroupBy(g => g.Name).ToList();
        Console.WriteLine(list);
        Console.WriteLine(list.Count);
    }

    class ItermsPerPerson 
    {
        public ItermsPerPerson(string name, string itemType)
        {
            Name = name;
            ItemType = itemType;
        }

        public string Name {get; set;}

        public string ItemType {get; set;}
    }
}

Give the above example I would like to group by name and merge item type, so the resulting list would contain two items with something similar to:

Name    ItemType
====================
"Joe"   "Pens, Cycle"
"Jim"   "Pencils"

TIA and Apologies if this has come up before, all searches with similar keywords on here resulted in code which grouped and sum/count another property, which is not what I am after.

Upvotes: 1

Views: 814

Answers (2)

Gilad Green
Gilad Green

Reputation: 37299

So by the example all you want to do with the ItemTpye is concatenate the values. For that add a projection (.Select) to change each grouping item to match that:

var list = authors.GroupBy(g => g.Name)
                  .Select(g => new ItermsPerPerson(g.Key, string.Join(", ", g.Select(i => i.ItemType))));

You could also use the overload of GroupBy to select what is kept for each item:

var list = authors.GroupBy(k => k.Name, v => v.ItemType)
                          .Select(g => new ItermsPerPerson(g.Key, string.Join(", ", g)));

Upvotes: 1

Antoine V
Antoine V

Reputation: 7204

You can use GroupBy and then recreate the ItermsPerPerson by a concatenate the ItemType

var rs = authors.GroupBy(c => c.Name)
       .Select(g => new ItermsPerPerson(g.Key, string.Join(",", g.Select(r => r.ItemType))) 

Upvotes: 1

Related Questions