标签:style blog ar color 使用 sp 数据 div on
不管在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 }
标签:style blog ar color 使用 sp 数据 div on
原文地址:http://www.cnblogs.com/yinhaichao/p/4106588.html