Reputation: 2010
I'm not really sure, why grouping by IEnumerable<string>
does not work. I provide custom IEqualityComparer, of course.
public class StringCollectionEqualityComparer : EqualityComparer<IEnumerable<string>>
{
public override bool Equals(IEnumerable<string> x, IEnumerable<string> y)
{
if (Object.Equals(x, y) == true)
return true;
if (x == null) return y == null;
if (y == null) return x == null;
return x.SequenceEqual(y, StringComparer.OrdinalIgnoreCase);
}
public override int GetHashCode(IEnumerable<string> obj)
{
return obj.OrderBy(value => value, StringComparer.OrdinalIgnoreCase).Aggregate(0, (hashCode, value) => value == null ? hashCode : hashCode ^ value.GetHashCode() + 33);
}
}
class A
{
public IEnumerable<string> StringCollection { get; set; }
}
IEnumerable<A> collection = // collection of A
var grouping = collection.GroupBy(obj => a.StringCollection, StringCollectionEqualityComparer.Default).ToList();
(ToList()
is to force evaluation, I have breakpoints in StringCollectionEqualityComparer
, but unfortunately, they're not invoked, as expected)
When I group collection
in this dumb way, it actually works.
var grouping = collection.GroupBy(obj => String.Join("|", obj.StringCollection));
Unfortunately, obviously it is not something I want to use.
By not working, I mean the results are not the ones I expect (using dumb way, the results are correct).
Upvotes: 0
Views: 242
Reputation:
StringCollectionEqualityComparer.Default
is a valid alternative way to access EqualityComparer<IEnumerable<string>>.Default
, since the latter is a base class of the former. You need to create an instance of StringCollectionEqualityComparer
, simply using new StringCollectionEqualityComparer()
, instead.
Upvotes: 6