zoukankan      html  css  js  c++  java
  • List的 Select()使用方法 Demo

    List的 Select()使用方法

    用List存储对象,代码如下:

    IList<Student> studentList = new List<Student>();
    for(int i=0;i<1000;i++)
    {
       Student student = new Student();
        //赋值
        studentList.Add(student);
    }

    现在需要从studentList中查询符合条件的对象,Student中有个ClassName字段,需要从studentList中查询ClassName为“高一(3)班”的所有学生,一般是这样做的:

    foreach(Student student in studentList)
    {
      if(student.ClassName=="高一(3)班")
        {
          newList.Add(student);
          continue;
          }
    }

    但这种方法比较笨重,请看如下的方法。。

    var newList = studentList.Where(s=>s.ClassName=="高一(3)班").ToList();

     另一种方法:

        List<Student> stu = studentList.FindAll(delegate(Student student)
        {
            if (student.Name.Equals("高一(3)班"))
            {
                return true;
            }
            else
            {
                return false;
            }
        });
  • 相关阅读:
    tomcat启动报错host-manager does not exist
    jq对象,js对象,dom对象的转化
    Axure
    css盒子垂直居中
    数组去重个人总结的六种方式
    原生Ajax
    tp5总结(四)
    tp5总结(二)
    tp5总结(三)
    tp5总结(一)
  • 原文地址:https://www.cnblogs.com/wolfocme110/p/4848535.html
Copyright © 2011-2022 走看看