Remove Duplicates From Strongly Typed Collections With Extension Methods

 Just a quick note to myself in case I begin the horrendous search for this again, the best / most performant way I have found to remove duplicates follows this structure:

[csharp]
public static class Extensions
{
public static IEnumerable RemoveDuplicates(this IEnumerable source)
{
return RemoveDuplicates(source, (t1, t2) => t1.Equals(t2));
}

public static IEnumerable RemoveDuplicates(this IEnumerable source, Func equater)
{
var result = new List();
foreach (var item in source.Where(item => result.All(t => !equater(item, t))))
{
result.Add(item);
}
return result;
}
}
[/csharp]

Share

Leave a Reply

Your email address will not be published. Required fields are marked *