-1

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.

Amirali Sam
  • 387
  • 9
  • 21
  • `person p1 = new person(); p1.name = "Bill"; p1.family = "Gates"; person p2 = new person(); p1.name = "Satoshi"; p1.family = "Nakamoto"; person p3 = new person(); p1.name = "AmirAli"; p1.family = "Sam";` is here a typo? you only set name and family to p1 Person – demo Jun 03 '21 at 07:17
  • @demo Really sorry! Disadvantage of copy pasting! But I still has the issue, I did it in another project and still not work so I simulated my project as shown. thanks. – Amirali Sam Jun 03 '21 at 07:25
  • The only real issue in your code is that you are iterating each list within the collection of groups, when you could just be iterating the collection of groups and displaying the key. See duplicate for the correct way to approach this. That said, I'll note that the next problem you're probably going to run into is that while the above works fine for grouping, it's only because you're using the same _instance_ for each `result` that uses the same `person`. ... – Peter Duniho Jun 03 '21 at 07:28
  • ... It's likely that in a real-world situation, you'll have multiple `person` instances that use the same values and will want to implement `IEquatable` for grouping to work like you expect it to. – Peter Duniho Jun 03 '21 at 07:28

2 Answers2

2

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);
}
demo
  • 6,038
  • 19
  • 75
  • 149
Alex Spitz
  • 85
  • 7
1

You assigned all Name and Family to the same variable p1

  • Really sorry! Disadvantage of copy pasting! But I still has the issue, I did it in another project and still not work so I simulated my project as shown. thanks – Amirali Sam Jun 03 '21 at 07:25