C# Lambda的简单应用的记录

在C#的List集合中,我们时常需要使用到大量的运算或者筛选等操作,按常规的方式无非就是利用foreach或者for对List集合进行循环操作,最后运算出结果。此种方法往往需要写多行语句,阅读性稍微差点,而Lambda表达式一条语句完成。

 先假定好我们待会使用的范例的格式:

 studentList对象:此对象是一个List集合,集合中的对象为学生实体Student。此集合中存放着整个学校学生的信息。

 scoreList对象:此对象是个List集合,集合中的对象是成绩实体Score,此集合中存放着为学生的成绩信息。

 Student实体:此实体包含下列几个属性,StudentName,StudentCode,ClassCode,ClassName,BirthDay,Grade。以上几个英文单词都比较简单,就不做解释了。

上代码 这里的操作就是 创建实体类 List列表 然后 Find方法 ob查找即可

        static void Main(string[] args)
        {
    
    
            List<Student> studentslist = new List<Student>();
            Student student = new Student();
            student.id = "20";
            student.name = "欢少";
            student.sex = "男";
            student.age = "20";
            studentslist.Add(student);

            Student student1 =studentslist.Find(ob => ob.id == "20");
            Console.WriteLine("Hello World!");
        }

猜你喜欢

转载自blog.csdn.net/weixin_44907128/article/details/107358531