Linq DistinctExtensions(linq根据条件去重)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static class DistinctExtensions { public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) { HashSet<TKey> keys = new HashSet<TKey>(); foreach (TSource element in source) if (keys.Add(keySelector(element))) yield return element; } }
使用方式
tmpList.DistinctBy(m => m.Id);
or
tmpList.DistinctBy(m => m.Name);