score:0

Accepted answer

The problem is that the lists are not exactly identical. It's comparing equality for grouping, and you have two new List<string>s, which aren't exactly equal. You can, however, join the strings by hash code, which would produce a correct result:

tests.GroupBy(t => String.Join(string.Empty, t.Children.Select(c => c.GetHashCode().ToString())));

score:0

I don't think there's a built in method to that.

Look at Jon Skeet's answer here:

Any chance to get unique records using Linq (C#)?

score:2

The reason you get 3 groups is because List<T> implements equality with default reference-equality, not by considering the "sequence equality" of the contained elements between any two lists. If you want such semantics, you'll have to implement an IEqualityComparer<IList<T>> (or similar) yourself and inject that into the GroupBy query using the overload that accepts an equality-comparer. Here's a sample implementation (for arrays, not lists, but easily adaptable).

If you're comfortable with set equality (order and duplicates are irrelevant), you're in luck: you can directly use HashSet<T> and the provided CreateSetComparer method for the comparer implementation:

  var t1 = new Test { Children = new HashSet<string> { "one", "two" } };
  var t2 = new Test { Children = new HashSet<string> { "one", "two" } };
  var t3 = new Test { Children = new HashSet<string> { "one", "three" } };

  var tests = new List<Test> { t1, t2, t3 };

  // Only two groups: { one, two } and { one, three }
  tests.GroupBy(t => t.Children, HashSet<string>.CreateSetComparer())
       .Dump();

score:4

By default, GroupBy is going to use reference equality when grouping by lists (which are reference types).

Since you've got new instances of the list each time, they are not equal.

However, there is an overload of GroupBy which lets you specify a custom IEqualityComparer, so that you can implement your own way of comparing a list of strings, for example.

To implement this, there are many other threads here about comparing two lists.


Related Query