通過Linq查找重復數據


一、模擬初始化集合數據

 List<Student> students = new List<Student>()
            {
             new Student(){  Age=18,Name="jom"}, new Student(){ Age=28,Name="bom"}, new Student(){ Age=18,Name="day"}, new Student(){ Age=38,Name="jim"}, new Student(){ Age=38,Name="lxsh"}, };

二、將集合數據按照年齡分組並取出分組大於1的

  var sameAges = students.GroupBy(g => g.Age).Where(s => s.Count() > 1).ToList();

三、將年齡相同的打印出來

 sameAges.ForEach(s => {
                               Console.WriteLine($"*******年齡為{s.Key}一共{ s.Count()}人*******"); s.ForEach(o => Console.WriteLine(o.Name)); });

四、運行效果

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM