程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> List擴展方法匯總(僅備注),list擴展匯總備注

List擴展方法匯總(僅備注),list擴展匯總備注

編輯:C#入門知識

List擴展方法匯總(僅備注),list擴展匯總備注


不管在c/s還是b/s的c#語言開發程序中,經常會用到List的擴展方法,尤其在json格式的數據和服務端交互越來越流行,很多時候總是在開發使用到的時候才去搜索有些擴展方法或者linq的用法,在這裡,我們只是做一個備注

因為linq沒有必要系統的學習,我們只要簡單的在list的擴展會使用即可,至少我只這麼認為的

本文沒有任何技術性,只是備注list或者list泛型的擴展方法,不至於在您不熟悉但是又使用到的時候到處搜索

 1  public sealed class Employee
 2     {
 3         public string Name { get; set; }
 4         public double Salary { get; set; }
 5         public short Dependents { get; set; }
 6     }
 7     public class Test
 8     {
 9         public void FunTest()
10         {
11             var employees = new List<Employee>
12              {
13                     new Employee { Name = "Bob", Salary = 1, Dependents = 0 },
14                     new Employee { Name = "Sherry", Salary = 2, Dependents = 1 },
15                     new Employee { Name = "Kathy", Salary = 3, Dependents = 0 },
16                     new Employee { Name = "Joe", Salary = 4, Dependents = 2 },
17                     new Employee { Name = "Bob", Salary = 5, Dependents = 0 },
18                     new Employee { Name = "Bob", Salary = 6, Dependents = 0 }
19              };
20             //泛型方法的類型參數是可以省略的
21 
22             //所有求和
23             double sumSalary = employees.Sum<Employee>(e => e.Salary);//21.0
24             //【linq方式】所有求和
25             var sumSalary2 = (from v in employees select v).Sum(e => e.Salary);//21.0

26 //按照某一條件求和 27 double sumSalaryFilter = employees.Where<Employee>(e => e.Name == "Bob").ToList().Sum(e => e.Salary);//12.0 28 //【linq方式】按照某一條件求和 29 var sumSalaryFilter2 = (from v in employees where v.Name == "Bob" select v).Sum(e => e.Salary);//12.0 30
31 //選擇數據而已,即返回自定義改變後的新的投影的數據列表(無法改變源數據) 32 var selectChangedNameList = employees.Select<Employee, Employee>(e => { return new Employee { Name = "Bob" }; }).ToList(); 33 //【linq方式】選擇數據而已,即返回自定義改變後的新的投影的數據列表(無法改變源數據) 34 var selectChangedNameList2 = (from v in employees select new { Name = "Bob", Salary = v.Salary, Dependents = v.Dependents }).ToList(); 35 //選擇返回某一列的清單投影的數據列表 36 var selectSalaryList = employees.Select<Employee, double>(e => { return e.Salary; }).ToList(); 37
38 //查找符合條件的第一個元素 39 Employee employee1 = employees.Find(e => e.Name == "Bob"); 40 //【linq方式】查找符合條件的第一個元素 41 Employee employee2 = (from v in employees where v.Name == "Bob" select v).First(); 42 //不用lambda方式篩選符合條件的元素列表 43 Predicate<Employee> aaaa = new Predicate<Employee>(fun1); 44 List<Employee> employee1List = employees.FindAll(aaaa); 45 //lambda方式篩選符合條件的元素列表 46 List<Employee> employee2List = employees.FindAll(e => e.Name == "Bob"); 47 //【linq方式】方式篩選符合條件的元素列表 48 List<Employee> employee2List2 = (from v in employees where v.Name == "Bob" select v).ToList();

49 //max,min,平均值,篩選滿足某條件的max和min已經使用linq方式 50 employees.Max(e => e.Salary); 51 employees.Min(e => e.Salary); 52 (from v in employees select v).Max(e => e.Salary); 53 (from v in employees select v).Min(e => e.Salary); 54 double maxSalaryFilter = employees.Where<Employee>(e => e.Name == "Bob").ToList().Max(e => e.Salary);
55 //簡單的運算,比如列表總的相加的值 56 var aggregateEmployee = employees.Aggregate<Employee>((result, next) => new Employee { Salary = result.Salary + next.Salary });

57 //某一列的不重復的匯總 58 var groupEmployees = employees.GroupBy(e => new { e.Name }).Select(g => new { g.Key, Count = g.Count() }).ToList(); 59 var groupEmployees1 = employees.GroupBy(e => e.Name).Select(e => new { Name = e.Key, sumSalary = e.Sum(d => d.Salary) }).ToList(); 60 var groupEmployees2 = (from v in employees group v by v.Name into g select new { Name = g.Key, SumSalary = g.Sum(d => d.Salary) }).ToList();

61 //查詢滿足某一條件的所有數據 62 IEnumerable<Employee> employee3List = employees.Where<Employee>(e => e.Name == "Bob"); 63 List<Employee> employee4List = employees.Where<Employee>(e => e.Name == "Bob").ToList(); 64 employee3List = employee4List as IEnumerable<Employee>; 65 66 Console.WriteLine("adfadsfasdfasdf"); 67 } 68 69 70 private bool fun1(Employee e) 71 { 72 return (e.Name == "Bob"); 73 } 74 }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved