To remove duplicate elements in a list, you can use
Distinct
. However, for Distinct to work on a list of lists, you'll have to create your own
IEqualityComparer
for two lists and pass that as argument to
Distinct
:
class ListEqualityComparer<T> : IEqualityComparer<List<T>>
{
public bool Equals(List<T> list1, List<T> list2)
{
if (list1 == list2) return true;
if (list1 == null || list2 == null) return false;
return Enumerable.SequenceEqual(list1, list2);
}
public int GetHashCode(List<T> list)
{
unchecked
{
int hash = 19;
foreach (var foo in list)
{
hash = hash * 31 + foo.GetHashCode();
}
return hash;
}
}
}
Then, you can use
Distinct
like this:
var similarLists = theMainList.Distinct(new ListEqualityComparer<YOUR SUB-LIST TYPE HERE>()).ToList();