标签:stat gets sum 参数 static code 方法 func 大于
public class Student { public int Id { get; set; } public string Name { get; set; } public int ClassId { get; set; } public int Age { get; set; } } public static class StudentManager { public static List<Student> students = new List<Student>() { new Student(){ Id=1,Name="张三",ClassId=1001,Age=15 }, new Student(){ Id=2,Name="李四",ClassId=1001,Age=15 }, new Student(){ Id=3,Name="王五",ClassId=1001,Age=15 }, new Student(){ Id=4,Name="赵六",ClassId=1001,Age=15 }, new Student(){ Id=5,Name="杨幂",ClassId=1001,Age=14 }, new Student(){ Id=6,Name="范冰冰",ClassId=101,Age=14 }, new Student(){ Id=7,Name="张学友",ClassId=1021,Age=14}, new Student(){ Id=8,Name="张三1",ClassId=1021,Age=16 }, new Student(){ Id=9,Name="张三2",ClassId=1001,Age=17 }, new Student(){ Id=10,Name="张三3",ClassId=1001,Age=15 }, new Student(){ Id=11,Name="张三4",ClassId=1001,Age=19 }, new Student(){ Id=12,Name="张三5",ClassId=1001,Age=25 }, new Student(){ Id=13,Name="张三6",ClassId=1003,Age=25 }, new Student(){ Id=14,Name="张三7",ClassId=1003,Age=25 }, new Student(){ Id=15,Name="张三8",ClassId=1003,Age=25 }, new Student(){ Id=16,Name="张三9",ClassId=1003,Age=25 }, new Student(){ Id=17,Name="张三0",ClassId=1003,Age=25 }, new Student(){ Id=18,Name="张三11",ClassId=1003,Age=15 }, new Student(){ Id=19,Name="张三a",ClassId=1011,Age=15 }, new Student(){ Id=20,Name="张三b",ClassId=1011,Age=15 }, new Student(){ Id=21,Name="张三c",ClassId=1011,Age=15 }, new Student(){ Id=22,Name="张三d",ClassId=1011,Age=15 }, new Student(){ Id=23,Name="张三e",ClassId=1011,Age=15 }, new Student(){ Id=24,Name="张三f",ClassId=1011,Age=15 }, new Student(){ Id=25,Name="张三g",ClassId=3001,Age=15 }, new Student(){ Id=26,Name="张三h",ClassId=3001,Age=13 }, new Student(){ Id=27,Name="张三i",ClassId=3001,Age=13 }, new Student(){ Id=28,Name="张三j",ClassId=3001,Age=13 }, new Student(){ Id=29,Name="张三k",ClassId=3001,Age=13 }, }; public static List<Student> FindStudents(Func<Student,bool> func) { List<Student> stus = new List<Student>(); foreach (var item in students) { if (func(item)) { stus.Add(item); } } return stus; } /// <summary> /// 查找ClassId为3001的学生 /// </summary> /// <param name="student">学生</param> /// <returns>是否为3001班级的学生</returns> public static bool GetClassId(Student student) { if (student.ClassId==3001) { return true; } return false; } /// <summary> /// 年龄大于20的学生 /// </summary> /// <param name="student"></param> /// <returns></returns> public static bool GetBigAge(Student student) { if (student.Age>20) { return true; } return false; } /// <summary> /// 年龄大于15 并且ClassId为1021 /// </summary> /// <param name="student"></param> /// <returns></returns> public static bool GetStuByClassIdAndAge(Student student) { if (student.Age > 15 && student.ClassId==1021) { return true; } return false; } }
下面这个是在Main方法中执行查询学生
//List<Student> stus = StudentManager.students; //Console.WriteLine("姓名---年龄---班级--编号"); //foreach (var item in stus) //{ // Console.WriteLine(item.Name+"---"+item.Age+"---"+item.ClassId+"---"+item.Id); //} List<Student> stus1= StudentManager.FindStudents(StudentManager.GetStuByClassIdAndAge); Console.WriteLine("姓名---年龄---班级--编号"); foreach (var item in stus1) { Console.WriteLine(item.Name + "---" + item.Age + "---" + item.ClassId + "---" + item.Id); }
标签:stat gets sum 参数 static code 方法 func 大于
原文地址:https://www.cnblogs.com/Rookieflying/p/10386198.html