public static IQueryable<TSource> DistinctBy<TSource, TKey>( this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector) { return source.GroupBy(keySelector).Select(grouping => grouping.FirstOrDefault()); }