例子

  List<student> pr = new List<student>();
            pr.Add(new student { id=1,name="张三",sex="男",age=10});
            pr.Add(new student { id = 1, name = "八七", sex = "男", age = 28 });
            pr.Add(new student { id = 2, name = "赵六", sex = "女", age =16 });
            pr.Add(new student { id = 3, name = "王五", sex = "男", age = 26 });
            pr.Add(new student { id = 4, name = "张四", sex = "女", age = 17 });
            List<student> hh = new List<student>();
            for (int i = 0; i < pr.Count; i++)
            {

                if (pr[i].age > 20)
                {

                    hh.Add(pr[i]);
                
                
                }
           
            
            }

            for (int i = 0; i < hh.Count; i++)
            {

                Console.WriteLine(hh[i]);
            
            
            }


        }
    }
}

public class student
    {

        public int id { get; set; }
        public string name { get; set; }
        public string sex { get; set; }
        public int age { get; set; }
        public override string ToString()
        {


            return string.Format("编号{0},姓名{1},性别{2},年龄{3}", this.id, this.name, this.sex, this.age);
        }
    }
}


2.


            List<student> pr = new List<student>();
            pr.Add(new student { id=1,name="张三",sex="女",age=18});
            pr.Add(new student { id = 1, name = "八七", sex = "男", age = 28 });
            pr.Add(new student { id = 2, name = "赵六", sex = "女", age = 16 });
            pr.Add(new student { id = 3, name = "王五", sex = "男", age = 26 });
            pr.Add(new student { id = 4, name = "张四", sex = "女", age = 17 });
            List<student> hh = getset(pr);
            for (int i = 0; i < hh.Count; i++)
            {

                Console.WriteLine(hh[i]);
            
            }
          
         
         

        }
        public static List<student> getset(List<student> pr)
        {

            List<student> hh = new List<student>();
            for (int i = 0; i < pr.Count; i++)
            {

                if (pr[i].age > 20)
                {


                    hh.Add(pr[i]);

                }
                
            }return hh;




3.

  List<Student> students = new List<Student>();
            //Where ToList  FirstOrDefault  
            students.Add(new Student { Id=1,Name="张三",Sex="男",Age=18});
            students.Add(new Student { Id = 2, Name = "李四", Sex = "女", Age = 28 });
            students.Add(new Student { Id = 3, Name = "王五", Sex = "男", Age = 16 });
            students.Add(new Student { Id =4, Name = "赵六", Sex = "女", Age = 26 });
            students.Add(new Student { Id = 5, Name = "田七", Sex = "男", Age = 27 });
            List<Student> result = students.OrderByDescending(s => s.Id).ToList();
            for (int i = 0; i < result.Count; i++)
            {
                Console.WriteLine(result[i]);
            }
          

  

posted @ 2016-01-27 16:21  西瓜冰镇老板  阅读(144)  评论(0编辑  收藏  举报