0

I have a list of tuple : List<Tuple<string,string,string>>

When I run my program, my List have this :

List[0] : 'Cars','Bird','1.0';
List[1] : 'Plane','Flower','2.0';
List[2] : 'Cars','Bird','1.0';
List[3] : 'Plane','Flower','2.0';

I want to make a group by in linq on my List to have only two lines :

List[0] : 'Cars','Bird','1.0';
List[1] : 'Plane','Flower','2.0';

They are the same, so I don't need to repeat it.

I already tried a lot of things. I Know how to make a groupby on a column.

But on a Tuple of 3 strings, I don't know.

During my tests, I have 4 lines. I only want Two lines as explained.

Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49
  • 3
    `List> result = source.Distinct().ToList();` – Dmitry Bychenko Jul 17 '19 at 13:31
  • 1
    I don't understand the question. What did you tried? https://dotnetfiddle.net/BbN7Pj. Both Distinct and GroupBy works. What was you error on your try? Mind sharing a simple [mcve] simple. – Drag and Drop Jul 17 '19 at 13:35
  • As We are kind of colleague (I worked in as a teach for a "centre de formation professionnel"), here is by best advice. 1/; Read [Ask] and [mcve], it's usefull tool that I still use in every day professional life. You can even translate them for the student. Being clear and precise and providing a demo pieces of code instead of a whole solution will be an improvement for them. 2/; Swap your name to an Alias- Students can act weirdly when they find us. – Drag and Drop Jul 17 '19 at 14:00
  • Related question : https://stackoverflow.com/questions/46693620/apply-distinct-on-the-basis-of-item2-of-list-of-integer-tuples-in-c-sharp – Drag and Drop Jul 17 '19 at 14:02

4 Answers4

1

If you really have List<Tuple<string, string, string>>, you could do just myList.Distinct().

var myList = new List<Tuple<string, string, string>> {
 Tuple.Create("a", "b", "1"),
 Tuple.Create("aa", "bb", "1"),
 Tuple.Create("a", "b", "1"), 
 Tuple.Create("aa", "bb", "1")};

myList.Distinct().Dump();   

enter image description here

Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49
0

You actually don't need a GroupBy function, but a Distinct function.

Tuples support equality checks, therefore in order to remove all the duplicates you can just write:

var listWithoutDuplicates = myList.Distinct().ToList();


If it weren't tuples or another type that implements the IEquatable<T> generic interface, you would want to use a DistinctBy function, that since it doesn't have a native .NET implementation, you can read more about here.

Tommaso Bertoni
  • 2,333
  • 1
  • 22
  • 22
0

If you want to group by all columns then you can use just Distinct extension method.

list.Distinct();

If you want to group by single column:

    List<Tuple<string, string, string>> list = new List<Tuple<string, string, string>>();
    list.Add(Tuple.Create("Cars", "Bird", "1.0"));
    list.Add(Tuple.Create("Plane", "Flower", "1.0"));
    list.Add(Tuple.Create("Cars", "Bird", "2.0"));
    list.Add(Tuple.Create("Plane", "Flower", "2.0"));

    foreach (var group in list.GroupBy(tuple => tuple.Item1))
    {
        Console.WriteLine(group.Key);

        foreach (var tuple in group)
        {
            Console.WriteLine("  " + tuple);
        }
    }

The result is:

Cars
  (Cars, Bird, 1.0)
  (Cars, Bird, 2.0)
Plane
  (Plane, Flower, 1.0)
  (Plane, Flower, 2.0)
fdafadf
  • 809
  • 5
  • 13
0

You can group by Item1,Item2 & Item3

var myList = new List<Tuple<string, string, string>> {
 Tuple.Create("a", "b", "1"),
 Tuple.Create("aa", "bb", "1"),
 Tuple.Create("a", "b", "1"), 
 Tuple.Create("aa", "bb", "1")};

var result = myList.GroupBy(x => new { x.Item1, x.Item2,x.Item3})