当前位置:编程学习 > ASP.NET> 正文

ASP.NET List 集合操作

时间:2014-12-2类别:编程学习

ASP.NET List 集合操作

ASP.NET List 集合操作

下面代码记录ASP.NET中对List 集合简单的操作

  •  
  •  
  • C# 代码   复制
  • 
    
        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");
            }
        }
    
    			

     

  •  

    标签:
    上一篇下一篇

    猜您喜欢

    热门推荐