List拡張メソッド要約(コメントのみ)
16702 ワード
c/sやb/sのc#言語開発プログラムでは、リストの拡張方法がよく使われています.特にjson形式のデータとサービス側のインタラクションがますます流行しています.開発して使ったときに拡張方法やlinqの使い方を検索することが多いです.ここでは、コメントをします.
linqはシステムの学習を必要としないので、listの拡張で簡単に使えばいいのですが、少なくとも私はそう思っています.
本文はいかなる技術性がなくて、ただlistあるいはlistの汎用的な拡張方法を注釈して、あなたが熟知していないでまた使う時至る所検索することはありません
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 }