ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

Distinct

Provides a Distinct method that takes a key selector lambda as parameter. The .net framework only provides a Distinct method that takes an instance of an implementation of IEqualityComparer<T> where the standard parameterless Distinct that uses the default equality comparer doesn't suffice.

Source

public static IEnumerable<T> Distinct<T, TKey>(this IEnumerable<T> @this, Func<T, TKey> keySelector)
{
   return @this.GroupBy( keySelector ).Select( grps => grps ).Select( e => e.First() );
}

Example

var instrumentSet = _instrumentBag.Distinct(i => i.Name);

Author: Martin Rosén-Lidholm

Submitted on: 21 dec 2010

Language: C#

Type: System.Collections.Generic.IEnumerable<T>

Views: 11385