You can use Distinct extension method from LINQ
In .Net 2.0 I`m pretty sure about this solution:
public IEnumerable<T> Distinct<T>(IEnumerable<T> source)
{
List<T> uniques = new List<T>();
foreach (T item in source)
{
if (!uniques.Contains(item)) uniques.Add(item);
}
return uniques;
}
Apart from the Distinct
extension method of LINQ, you could use a HashSet<T>
object that you initialise with your collection. This is most likely more efficient than the LINQ way, since it uses hash codes (GetHashCode
) rather than an IEqualityComparer
).
In fact, if it's appropiate for your situation, I would just use a HashSet
for storing the items in the first place.
Source: Stackoverflow.com