Thomas Daniels
Чтобы удалить повторяющиеся элементы в списке, вы можете использовать Distinct
Однако для того, чтобы Distinct работал со списком списков, вам придется создать свой собственный IEqualityComparer
для двух списков и передайте это в качестве аргумента Distinct
:
class ListEqualityComparer<T> : IEqualityComparer<List<T>>
{
public bool Equals(List<T> list1, List<T> list2)
{
if (list1 == list2) return true; // quick check for reference equality
if (list1 == null || list2 == null) return false; // One list is null and the other is not null. Return false already so we aren't passing 'null' to SequenceEqual below.
return Enumerable.SequenceEqual(list1, list2);
}
public int GetHashCode(List<T> list)
{
// See here for information about a 'hash code': https://msdn.microsoft.com/en-us/library/system.object.gethashcode(v=vs.110).aspx
// I have taken the code below from Jon Skeet's Stack Overflow answer https://stackoverflow.com/a/8094931
unchecked
{
int hash = 19;
foreach (var foo in list)
{
hash = hash * 31 + foo.GetHashCode();
}
return hash;
}
}
}
Затем вы можете использовать
Distinct
подобный этому:
var similarLists = theMainList.Distinct(new ListEqualityComparer<YOUR SUB-LIST TYPE HERE>()).ToList();