use*_*618 152 c# linq distinct
我有一个集合:
List<Car> cars = new List<Car>();
Run Code Online (Sandbox Code Playgroud)
汽车是由他们的财产唯一标识CarCode.
我收集了三辆汽车,两辆汽车编码相同.
如何使用LINQ将此集合转换为具有唯一CarCodes的Cars?
Guf*_*ffa 268
您可以使用分组,并从每个组获得第一辆车:
List<Car> distinct =
cars
.GroupBy(car => car.CarCode)
.Select(g => g.First())
.ToList();
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 116
使用MoreLINQ,它有一个DistinctBy方法:)
IEnumerable<Car> distinctCars = cars.DistinctBy(car => car.CarCode);
Run Code Online (Sandbox Code Playgroud)
(请注意,这仅适用于LINQ to Objects.)
She*_*ror 44
与Guffa相同的方法,但作为扩展方法:
public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> items, Func<T, TKey> property)
{
return items.GroupBy(property).Select(x => x.First());
}
Run Code Online (Sandbox Code Playgroud)
用作:
var uniqueCars = cars.DistinctBy(x => x.CarCode);
Run Code Online (Sandbox Code Playgroud)
Ant*_*ram 29
您可以实现IEqualityComparer并在您的Distinct扩展中使用它.
class CarEqualityComparer : IEqualityComparer<Car>
{
#region IEqualityComparer<Car> Members
public bool Equals(Car x, Car y)
{
return x.CarCode.Equals(y.CarCode);
}
public int GetHashCode(Car obj)
{
return obj.CarCode.GetHashCode();
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
然后
var uniqueCars = cars.Distinct(new CarEqualityComparer());
Run Code Online (Sandbox Code Playgroud)
Linq-to-Objects的另一种扩展方法,不使用GroupBy:
/// <summary>
/// Returns the set of items, made distinct by the selected value.
/// </summary>
/// <typeparam name="TSource">The type of the source.</typeparam>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <param name="source">The source collection.</param>
/// <param name="selector">A function that selects a value to determine unique results.</param>
/// <returns>IEnumerable<TSource>.</returns>
public static IEnumerable<TSource> Distinct<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)
{
HashSet<TResult> set = new HashSet<TResult>();
foreach(var item in source)
{
var selectedValue = selector(item);
if (set.Add(selectedValue))
yield return item;
}
}
Run Code Online (Sandbox Code Playgroud)
我认为性能方面(或任何术语)的最佳选择是使用IEqualityComparer接口进行区分.
虽然每次实现每个类的新比较器都很麻烦并且生成样板代码.
所以这里有一个扩展方法,可以为使用反射的任何类动态生成新的IEqualityComparer.
用法:
var filtered = taskList.DistinctBy(t => t.TaskExternalId).ToArray();
Run Code Online (Sandbox Code Playgroud)
扩展方法代码
public static class LinqExtensions
{
public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> items, Func<T, TKey> property)
{
GeneralPropertyComparer<T, TKey> comparer = new GeneralPropertyComparer<T,TKey>(property);
return items.Distinct(comparer);
}
}
public class GeneralPropertyComparer<T,TKey> : IEqualityComparer<T>
{
private Func<T, TKey> expr { get; set; }
public GeneralPropertyComparer (Func<T, TKey> expr)
{
this.expr = expr;
}
public bool Equals(T left, T right)
{
var leftProp = expr.Invoke(left);
var rightProp = expr.Invoke(right);
if (leftProp == null && rightProp == null)
return true;
else if (leftProp == null ^ rightProp == null)
return false;
else
return leftProp.Equals(rightProp);
}
public int GetHashCode(T obj)
{
var prop = expr.Invoke(obj);
return (prop==null)? 0:prop.GetHashCode();
}
}
Run Code Online (Sandbox Code Playgroud)