C# EF去除重复列DistinctBy
作者:互联网
1.添加一个扩展方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public static class DistinctByClass
{
public static IEnumerable<TSource> DistinctBy<TSource, TKey>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
HashSet<TKey> seenKeys = new HashSet<TKey>();
foreach (TSource element in source)
{
if (seenKeys.Add(keySelector(element)))
{
yield return element;
}
}
}
}
|
2.使用方法如下(针对ID,和Name进行Distinct)
var query = people.DistinctBy(p => new { p.Id, p.Name });
3.若仅仅针对ID进行distinct:
var query = people.DistinctBy(p => p.Id);转 https://www.cnblogs.com/hsjloveit/p/12509818.html
标签:Name,people,C#,EF,element,static,DistinctBy,keySelector 来源: https://www.cnblogs.com/wl-blog/p/16457795.html