ASP.NET List 集合操作
类别:编程学习 浏览量:1702
时间:2014-12-2 ASP.NET List 集合操作
ASP.NET List 集合操作下面代码记录ASP.NET中对List 集合简单的操作
public sealed class Employee { public string Name { get; set; } public double Salary { get; set; } public short Dependents { get; set; } } public class Test { public void FunTest() { var employees = new List<Employee> { new Employee { Name = "Bob", Salary = 1, Dependents = 0 }, new Employee { Name = "Sherry", Salary = 2, Dependents = 1 }, new Employee { Name = "Kathy", Salary = 3, Dependents = 0 }, new Employee { Name = "Joe", Salary = 4, Dependents = 2 }, new Employee { Name = "Bob", Salary = 5, Dependents = 0 }, new Employee { Name = "Bob", Salary = 6, Dependents = 0 } }; //泛型方法的类型参数是可以省略的 //所有求和 double sumSalary = employees.Sum<Employee>(e => e.Salary);//21.0 //【linq方式】所有求和 var sumSalary2 = (from v in employees select v).Sum(e => e.Salary);//21.0 //按照某一条件求和 double sumSalaryFilter = employees.Where<Employee>(e => e.Name == "Bob").ToList().Sum(e => e.Salary);//12.0 //【linq方式】按照某一条件求和 var sumSalaryFilter2 = (from v in employees where v.Name == "Bob" select v).Sum(e => e.Salary);//12.0 //选择数据而已,即返回自定义改变后的新的投影的数据列表(无法改变源数据) var selectChangedNameList = employees.Select<Employee, Employee>(e => { return new Employee { Name = "Bob" }; }).ToList(); //【linq方式】选择数据而已,即返回自定义改变后的新的投影的数据列表(无法改变源数据) var selectChangedNameList2 = (from v in employees select new { Name = "Bob", Salary = v.Salary, Dependents = v.Dependents }).ToList(); //选择返回某一列的清单投影的数据列表 var selectSalaryList = employees.Select<Employee, double>(e => { return e.Salary; }).ToList(); //查找符合条件的第一个元素 Employee employee1 = employees.Find(e => e.Name == "Bob"); //【linq方式】查找符合条件的第一个元素 Employee employee2 = (from v in employees where v.Name == "Bob" select v).First(); //不用lambda方式筛选符合条件的元素列表 Predicate<Employee> aaaa = new Predicate<Employee>(fun1); List<Employee> employee1List = employees.FindAll(aaaa); //lambda方式筛选符合条件的元素列表 List<Employee> employee2List = employees.FindAll(e => e.Name == "Bob"); //【linq方式】方式筛选符合条件的元素列表 List<Employee> employee2List2 = (from v in employees where v.Name == "Bob" select v).ToList(); //max,min,平均值,筛选满足某条件的max和min已经使用linq方式 employees.Max(e => e.Salary); employees.Min(e => e.Salary); (from v in employees select v).Max(e => e.Salary); (from v in employees select v).Min(e => e.Salary); double maxSalaryFilter = employees.Where<Employee>(e => e.Name == "Bob").ToList().Max(e => e.Salary); //简单的运算,比如列表总的相加的值 var aggregateEmployee = employees.Aggregate<Employee>((result, next) => new Employee { Salary = result.Salary + next.Salary }); //某一列的不重复的汇总 var groupEmployees = employees.GroupBy(e => new { e.Name }).Select(g => new { g.Key, Count = g.Count() }).ToList(); var groupEmployees1 = employees.GroupBy(e => e.Name).Select(e => new { Name = e.Key, sumSalary = e.Sum(d => d.Salary) }).ToList(); 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(); //查询满足某一条件的所有数据 IEnumerable<Employee> employee3List = employees.Where<Employee>(e => e.Name == "Bob"); List<Employee> employee4List = employees.Where<Employee>(e => e.Name == "Bob").ToList(); employee3List = employee4List as IEnumerable<Employee>; Console.WriteLine("adfadsfasdfasdf"); } private bool fun1(Employee e) { return (e.Name == "Bob"); } }
标签:List
您可能感兴趣
- python3目录操作(Python3 pip3 list 出现 DEPRECATION 警告的解决方法)
- dedecms自定义字段(详解怎么样让DEDECMS的list标签支持weight排序的方法)
- ubuntu中source指令(怎么修改Ubuntu的源列表source list详解)
- 数组、ArrayList和List的区别
- C#中ArrayList与Array的区别
- mysql索引的比较规则(MySQL的Flush-List和脏页的落盘机制)
- datatable和list的转换
- idea的tomcat怎么关闭(解决Idea的tomcat启动报多个listener的错误问题)
- python里list操作大全(Python列表List知识点总结)
- python中list怎么用(详解python中list的使用)
- python redis锁的使用(Python操作redis实例小结String、Hash、List、Set等)
- C#中SortedList的用法
- Dictionary转换为list
- dedecms投稿页面(dedecms artlist读取全站最新文章的代码)
- dedecms表单格式(DedeCMS arclist的分页使用教程)
- list使用linq排序
- 这个全椒人被通报表彰,看看你认识吗(这个全椒人被通报表彰)
- 全椒人,38年集体回忆 1980-2018 ,看完不要哭(全椒人38年集体回忆)
- 董元奔吟咏历代文人 1012新旧均可 全椒人张璪 1022 -1093(董元奔吟咏历代文人)
- 泪目 这位 刷屏 的英雄,是全椒人的骄傲(泪目这位刷屏)
- 人从众 火炎焱 全椒再现 正月十六走太平 的魅力(人从众火炎焱全椒再现)
- 官宣 全椒籍明星许海峰 奚秀兰 方芳 王璐瑶携手回家 走太平(全椒籍明星许海峰)