List extension method summary (remarks only), list extension summary remarks
The List extension method is often used in c/s or B/s c # programming programs, especially in json format data and server interaction, most of the time, I always search for some extension methods or the usage of linq when developing and using them. Here, we just make a note.
Because there is no need for systematic learning in linq, we only need to simply use the list extension, at least I think so.
This article does not have any technical skills. It just adds a list or list extension method, so you will not search for it when you are not familiar with it but are still using it.
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 // The type parameter of the generic method can be omitted 21 22 // All sums 23 double sumSalary = employees. sum <Employee> (e => e. salary); // 21.024 // [linq mode] All sums 25 var sumSalary2 = (from v in employees select v ). sum (e => e. salary); // 21.0.
26 // sum according to a certain condition 27 double sumSalaryFilter = employees. where <Employee> (e => e. name = "Bob "). toList (). sum (e => e. salary); // 12.028 // [linq mode] returns a sum of 29 var sumSalaryFilter2 = (from v in employees where v. name = "Bob" select v ). sum (e => e. salary); // 12.030.
31 // select data only, that is, the data list of the new projection after the custom change is returned (the source data cannot be changed) 32 var selectChangedNameList = employees. select <Employee, Employee> (e =>{ return new Employee {Name = "Bob "};}). toList (); 33 // [linq mode] Select data only, that is, the new projection data list after the custom change is returned (the source data cannot be changed) 34 var selectChangedNameList2 = (from v in employees select new {Name = "Bob", Salary = v. salary, Dependents = v. dependents }). toList (); 35 // select to return the list projection data list of a column 36 var selectSalaryList = employees. select <Employee, double> (e => {return e. salary ;}). toList (); 37
38 // search for the first qualified element 39 Employee employee1 = employees. find (e => e. name = "Bob"); 40 // [linq mode] searches for the first qualified element 41 Employee employee2 = (from v in employees where v. name = "Bob" select v ). first (); 42 // filter the list of qualified elements without lambda 43 Predicate <Employee> aaaa = new Predicate <Employee> (fun1 ); 44 List <Employee> employee1List = employees. findAll (aaaa); 45 // List of qualified elements in lambda mode 46 List <Employee> employee2List = employees. findAll (e => e. name = "Bob"); 47 // filter the List of elements that meet the conditions using the [linq method] method. 48 List <Employee> employee2List2 = (from v in employees where v. name = "Bob" select v ). toList ();
49 // max, min, and average. filter the values of max and min that meet certain conditions. Currently, 50 employees is used in the linq mode. 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 // simple operation, such as the total sum of 56 var aggresponemployee = employees. aggregate <Employee> (result, next) => new Employee {Salary = result. salary + next. salary });
57 // a non-repeated summary of a column 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 // query all data that meets a certain condition 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}