C# group by a list contains another class

366 Views Asked by At

I have two classes as below:

public class result
{
    public person identity { get; set; }
}

public class person
{
    public string name { get; set; }

    public string family { get; set; }
}

I have List<result> which has some repetitive data. I want to get rid of repetitive data by System.Linq.GroupBy

I searched a lot and I think the best answer I've found was here, so I have simulated my code as below:

class Program
{
    static void Main(string[] args)
    {
        person p1 = new person();
        p1.name = "Bill"; p1.family = "Gates";
        person p2 = new person();
        p2.name = "Satoshi"; p2.family = "Nakamoto";
        person p3 = new person();
        p3.name = "AmirAli"; p3.family = "Sam";

        List<result> lst = new List<result>();
        List<List<result>> lstGrp = new List<List<result>>();

        lst.Add(new result { identity = p1 });
        lst.Add(new result { identity = p1 });
        lst.Add(new result { identity = p1 });
        lst.Add(new result { identity = p2 });
        lst.Add(new result { identity = p2 });
        lst.Add(new result { identity = p2 });
        lst.Add(new result { identity = p3 });
        lst.Add(new result { identity = p3 });
        lst.Add(new result { identity = p3 });

        lstGrp = lst
            .GroupBy(r => r.identity)
            .Select(grp => grp.ToList())
            .ToList();

        foreach (List<result> list in lstGrp)
        {
            foreach (result item in list)
            {
                Console.WriteLine(item.identity.name + " " + item.identity.family);
            }
        }
    }
}

As shown, I have repetitive person in different result but I want to see each person once in my Console as:

enter image description here

But what I faced at last was:

enter image description here

How should I change my code to get the result I want?

Thanks a lot in advance.

2

There are 2 best solutions below

1
Alex Spitz On BEST ANSWER

I think that what you are really looking for is "distinct" and not "groupBy". See here Select distinct using linq .

https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.distinct?view=net-5.0

What they say about distinct:

Returns distinct elements from a sequence.


Example:

person p1 = new person();
p1.name = "Bill";
p1.family = "Gates";

person p2 = new person();
p2.name = "Satoshi";
p2.family = "Nakamoto";

person p3 = new person();
p3.name = "AmirAli";
p3.family = "Sam";

List<result> lst = new List<result>();
List<person> lstGrp = new List<person>();
lst.Add(new result{identity = p1});
lst.Add(new result{identity = p1});
lst.Add(new result{identity = p1});
lst.Add(new result{identity = p2});
lst.Add(new result{identity = p2});
lst.Add(new result{identity = p2});
lst.Add(new result{identity = p3});
lst.Add(new result{identity = p3});
lst.Add(new result{identity = p3});

lstGrp = lst.Select(r => r.identity).Distinct().ToList();
foreach (var item in lstGrp)
{
    Console.WriteLine(item.name + " " + item.family);
}
1
user2471755 On

You assigned all Name and Family to the same variable p1