关于List的操作

来源:互联网 发布:java method的反射 编辑:程序博客网 时间:2024/06/10 07:39

一 List的添加和遍历和删除和条件查找




二代码


 

using System.Collections.Generic;using System.Linq;class Program{    static void Main(string[] args)    {        int i = 0;        List<DEPT> list = new List<DEPT>();        list.Add(new DEPT() { DeptId = 1, DeptName = "外科", DoctorNum = 18 });//用list添加科室          list.Add(new DEPT() { DeptId = 1, DeptName = "肾内科", DoctorNum = 6, Desc = "肾内科描述" });        list.Add(new DEPT() { DeptId = 1, DeptName = "眼科", DoctorNum = 3, Desc = "眼科描述" });        System.Console.WriteLine("for循环遍历结果:");        for (i = 0; i < list.Count; i++)        {            System.Console.WriteLine("科室id = " + list[i].DeptId + " 医生人数 = " + list[i].DoctorNum + " 科室描述: " + list[i].Desc);        }        System.Console.WriteLine("找出医生数目大于6的科室:");         //System.Func<DEPT,bool> 表示函数的参数1个且类型为DEPT,返回值为bool类型        IEnumerable<DEPT> ienumerable = list.Where((dept) => { return dept.DoctorNum > 6; });//要添加这个引用  using System.Linq; 才可以使用Where();        foreach (DEPT Dept in ienumerable)        {            System.Console.WriteLine("科室id = " + Dept.DeptId + " 医生人数 = " + Dept.DoctorNum + " 科室描述: " + Dept.Desc);        }        System.Console.WriteLine("删除医生数目大于3的科室:");        list.RemoveAll((dept) => { return dept.DoctorNum>3; });          System.Console.WriteLine("foreach遍历结果:");        foreach (DEPT ObjDept in list)        {            System.Console.WriteLine("科室id = " + ObjDept.DeptId + " 医生人数 = " + ObjDept.DoctorNum + " 科室描述: " + ObjDept.Desc);        }        System.Console.ReadLine();    }}class DEPT//科室类  {    public int DeptId { get; set; }//科室id      public string DeptName { get; set; }//科室名字      public string Desc { get; set; }//描述信息      public int DoctorNum { get; set; }//医生人数  }




0 0
原创粉丝点击