發(fā)文章
發(fā)文工具
撰寫(xiě)
網(wǎng)文摘手
文檔
視頻
思維導(dǎo)圖
隨筆
相冊(cè)
原創(chuàng)同步助手
其他工具
圖片轉(zhuǎn)文字
文件清理
AI助手
留言交流
Distinct()方法在MSDN中的示例
public class Product { public string Name { get; set; } public int Code { get; set; } }
Product[] products = { new Product { Name = "apple", Code = 9 }, new Product { Name = "orange", Code = 4 }, new Product { Name = "apple", Code = 10 }, new Product { Name = "lemon", Code = 9 } }; var lstDistProduct = products.Distinct(); foreach (Product p in list1) { Console.WriteLine(p.Code + " : " + p.Name); }
但在實(shí)際使用中發(fā)現(xiàn)往往無(wú)法實(shí)現(xiàn)篩選出不重復(fù)的數(shù)據(jù)的目的
查找相關(guān)資料獲得如下方法:
方法1:使用MoreLinq庫(kù)
var list1 = products.DistinctBy(x=> x.Code); foreach (Product p in list1) { Console.WriteLine(p.Code + " : " + p.Name); }
方法2:
class ProductComparare : IEqualityComparer<product> { private Func<Product, object> _funcDistinct; public ProductComparare(Func<Product, object> funcDistinct) { this._funcDistinct = funcDistinct; } public bool Equals(Product x, Product y) { return _funcDistinct(x).Equals(_funcDistinct(y)); } public int GetHashCode(Product obj) { return this._funcDistinct(obj).GetHashCode(); } }
var list2 = products.Distinct(new ProductComparare( a => a.Code )); foreach (Product p in list2) { Console.WriteLine(p.Code + " : " + p.Name); }
方法3:
List<Product> list = products .GroupBy(a => a.Code ) .Select(g => g.First()) .ToList(); foreach (Product p in list) { Console.WriteLine(p.Code + " : " + p.Name); }
本人通過(guò)第三種方法得到了想要的不重復(fù)數(shù)據(jù)。
來(lái)自: 昵稱(chēng)10504424 > 《工作》
0條評(píng)論
發(fā)表
請(qǐng)遵守用戶 評(píng)論公約
LINQ學(xué)習(xí)(五):Select子句
} } List<Student> students = new List<Student>{ new Student {Name="Terry", Scores=new List<int> {97, 72, 81, 60}}, new Student {Name="AI", Scores=new...
linq Distinct的一個(gè)簡(jiǎn)單實(shí)現(xiàn)方法
linq Distinct的一個(gè)簡(jiǎn)單實(shí)現(xiàn)方法。linq Distinct.[html] view plain copy foreach (List<ReportName> reportNames in query1) { reportNameTemp = new ReportName();fore...
c#范型List的Sort方法詳解
SortTestObj1 sObj = (SortTestObj1)obj;第二種帶有比較器參數(shù)的Sort方法,List中的元素對(duì)象不需要繼承IComparable接口,但需要額外創(chuàng)建一個(gè)對(duì)象的比較器,下面的代碼中的SortTestObj2類(lèi)是準(zhǔn)備要保存到...
16 C# 第十四章 標(biāo)準(zhǔn)查詢(xún)運(yùn)算符
- Linq 函數(shù) where() 篩選 IEnumerable<Student> studentQuery = students.Where( student => student.S...
C#中IEnumerable<T>.Select()、SelectMany()的簡(jiǎn)單使用
var Lists = PersonLists.public static IEnumerable<TResult> SelectMany<TSource, TResult>(this IEnumerable<TSource...
C#(六)基礎(chǔ)篇—數(shù)組
C#(六)基礎(chǔ)篇—數(shù)組。//聲明一個(gè)用于存儲(chǔ)int類(lèi)型的一維數(shù)組并賦值 int[] array1 = new int[2]; array1[0] = 1; array1[1] = 2; //聲明同時(shí)賦值 int[] array2 = new int[] { 1, 9, 5, 7, 3 }; //另一...
IEnumerable的幾個(gè)簡(jiǎn)單用法
例如: public static void Print(IEnumerable myList) { int i = 0; foreach (Object obj in myList) { if (obj is Student)//這個(gè)是類(lèi)型的判斷,這里Student是一個(gè)類(lèi)或結(jié)構(gòu) { Student s = (Student)o...
C# List<T>用法
E.g.: List<string> mList = new List<string>();當(dāng)前 List 的元素被逐個(gè)傳遞給Predicate委托,并在 List 中向前移動(dòng),從第一個(gè)元素開(kāi)始,到最后一個(gè)元素結(jié)束。表示一個(gè)元素為int的鏈表in...
C#程序設(shè)計(jì)筆試題
public static int X;<FileSystem>< DriverC ><Dir DirName=”MSDOS622”><File FileName =” Command.com” ></File></Dir><File FileName =”MSDOS.SYS” &g...
微信掃碼,在手機(jī)上查看選中內(nèi)容