At the end of my current LINQ query, I have data which is in the form of an IEnumerable<IEnumerable<int>>, with several duplicate combinations.
For example:
{
{5, 20}
{5, 20}
{10, 15}
}
What's the best way to filter these so that each set is only represented once? A LINQ answer would be ideal so I can chain it further.
If you create a sequence equality comparer like this:
class SequenceEqualityComparer<T> : IEqualityComparer<IEnumerable<T>> {
public bool Equals(IEnumerable<T> a, IEnumerable<T> b) {
if (a == null) return b == null;
if (b == null) return false;
return a.SequenceEqual(b);
}
public int GetHashCode(IEnumerable<T> val) {
return val.Where(v => v != null)
.Aggregate(0, (h, v) => h ^ v.GetHashCode());
}
}
Then, you can call .Distinct(new SequenceEqualityComparer<int>()).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With