Linq distinct去重方法之一
生活随笔
收集整理的這篇文章主要介紹了
Linq distinct去重方法之一
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
var?result?=?query.Distinct().ToList(); List<DeliveryOrderViewModel> dov?=?result.GroupBy( p?=>?new?{p.SAP_DeliveryOrderID}).Select( g?=>?g.First()).ToList(); return?dov;一、使用Distinct()擴(kuò)展方法去重
?
實(shí)例:根據(jù)Id去重
錯(cuò)誤的方式
?
? ? List<Product> products = new List<Product>()
? ? {
? ? ? ? new Product(){ Id="1", Name="n1"},
? ? ? ? new Product(){ Id="1", Name="n2"},
? ? ? ? new Product(){ Id="2", Name="n1"},
? ? ? ? new Product(){ Id="2", Name="n2"},
? ? };
? ? var distinctProduct = products.Distinct();
返回4條數(shù)據(jù),因?yàn)镈istinct 默認(rèn)比較的是Product對(duì)象的引用
?
正確的方式
新建類ProductIdComparer,繼承 IEqualityComparer<Product>,實(shí)現(xiàn)Equals方法
?
?
C# 代碼? ?復(fù)制
public class ProductIdComparer : IEqualityComparer<Product>
{
? ? public bool Equals(Product x, Product y)
? ? {
? ? ? ? if (x == null)
? ? ? ? ? ? return y == null;
? ? ? ? return x.Id == y.Id;
? ? }
? ? public int GetHashCode(Product obj)
? ? {
? ? ? ? if (obj == null)
? ? ? ? ? ? return 0;
? ? ? ? return obj.Id.GetHashCode();
? ? }
}
?
使用的時(shí)候,只需要
var distinctProduct = allProduct.Distinct(new ProductIdComparer());
?
備注:現(xiàn)在假設(shè)我們要 按照 Name來篩選重復(fù)呢?則需要再添加一個(gè)類ProductNameComparer.
?
二、使用GroupBy方式去重
對(duì)需要Distinct的字段進(jìn)行分組,取組內(nèi)的第一條記錄這樣結(jié)果就是Distinct的數(shù)據(jù)了。
例如
?
List<Product> distinctProduct = allProduct
? .GroupBy(p => new {p.Id, p.Name} )
? .Select(g => g.First())
? .ToList();
?
三、通過自定義擴(kuò)展方法DistinctBy實(shí)現(xiàn)去重
?
?
C# 代碼? ?復(fù)制
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;
? ? ? ? }
? ? }
}
方法的使用
1、針對(duì)ID,和Name進(jìn)行Distinct
var query = allProduct.DistinctBy(p => new { p.Id, p.Name });
2、僅僅針對(duì)ID進(jìn)行distinct:
var query = allProduct.DistinctBy(p => p.Id);
?
實(shí)例:根據(jù)Id去重
錯(cuò)誤的方式
?
? ? List<Product> products = new List<Product>()
? ? {
? ? ? ? new Product(){ Id="1", Name="n1"},
? ? ? ? new Product(){ Id="1", Name="n2"},
? ? ? ? new Product(){ Id="2", Name="n1"},
? ? ? ? new Product(){ Id="2", Name="n2"},
? ? };
? ? var distinctProduct = products.Distinct();
返回4條數(shù)據(jù),因?yàn)镈istinct 默認(rèn)比較的是Product對(duì)象的引用
?
正確的方式
新建類ProductIdComparer,繼承 IEqualityComparer<Product>,實(shí)現(xiàn)Equals方法
?
?
C# 代碼? ?復(fù)制
public class ProductIdComparer : IEqualityComparer<Product>
{
? ? public bool Equals(Product x, Product y)
? ? {
? ? ? ? if (x == null)
? ? ? ? ? ? return y == null;
? ? ? ? return x.Id == y.Id;
? ? }
? ? public int GetHashCode(Product obj)
? ? {
? ? ? ? if (obj == null)
? ? ? ? ? ? return 0;
? ? ? ? return obj.Id.GetHashCode();
? ? }
}
?
使用的時(shí)候,只需要
var distinctProduct = allProduct.Distinct(new ProductIdComparer());
?
備注:現(xiàn)在假設(shè)我們要 按照 Name來篩選重復(fù)呢?則需要再添加一個(gè)類ProductNameComparer.
?
二、使用GroupBy方式去重
對(duì)需要Distinct的字段進(jìn)行分組,取組內(nèi)的第一條記錄這樣結(jié)果就是Distinct的數(shù)據(jù)了。
例如
?
List<Product> distinctProduct = allProduct
? .GroupBy(p => new {p.Id, p.Name} )
? .Select(g => g.First())
? .ToList();
?
三、通過自定義擴(kuò)展方法DistinctBy實(shí)現(xiàn)去重
?
?
C# 代碼? ?復(fù)制
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;
? ? ? ? }
? ? }
}
方法的使用
1、針對(duì)ID,和Name進(jìn)行Distinct
var query = allProduct.DistinctBy(p => new { p.Id, p.Name });
2、僅僅針對(duì)ID進(jìn)行distinct:
var query = allProduct.DistinctBy(p => p.Id);
轉(zhuǎn)載于:https://www.cnblogs.com/jhxk/articles/9789336.html
總結(jié)
以上是生活随笔為你收集整理的Linq distinct去重方法之一的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [AHOI2013]作业
- 下一篇: apps-privacy-policy