对泛型集合进行筛选
2012-02-28 15:30
232 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { public class Student { public Student() { } public Student(string name, int age) { this.name = name; this.age = age; } private string name; public string Name { get { return name; } set { name = value; } } private int age; public int Age { get { return age; } set { age = value; } } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { class Program { private static List<Student> list1 = new List<Student>(); //声明一个用于放置初始值的集合 注:必须声明List类型而不是IList接口类型 private static List<Student> list2 = new List<Student>(); //用户存放筛选结果 static void Main(string[] args) { //声明实例化student对象 Student stu1 = new Student("aa", 10); Student stu2 = new Student("bb", 13); Student stu3 = new Student("cc", 15); Student stu4 = new Student("dd", 18); //向集合中添加对象 list1.Add(stu1); list1.Add(stu2); list1.Add(stu3); list1.Add(stu4); Console.WriteLine(list1.Find(delegate(Student stu) { return stu.Age > 12; }).Name); //返回筛选满足条件的第一个对象 list2 = list1.FindAll(delegate(Student stu) { return stu.Age > 12; }); //对泛型集合进行筛选 //遍历结果 foreach (Student stu in list2) { Console.WriteLine(stu.Name); } } } }
泛型集合筛选的语法:
List<T> a=new List<T>();
List<T> b=new List<T>();
B=a.FindAll(delegate(T 对象t){return t.条件});
相关文章推荐
- 以泛型实现把集合按照n个一组进行平分
- 不使用遍历循环、Linq与Lamba表达式快速对List集合进行筛选与排序——在集合类里使用Find、FindAll与Sort
- C#中对List<Object>泛型集合,根据对象的某一属性进行升序排序
- siblings() 获得匹配集合中每个元素的同胞,通过选择器进行筛选是可选的。
- 使用了泛型的集合 进行存储 、遍历
- 深入理解 c# 第三章 用泛型方法将一个给定的值进行比较
- java基础之泛型和集合
- List集合,泛型,将元素全部添加到集合中
- STL中的set集合容器进行集合运算:并、交、差实例
- 按bean的属性值对list集合进行排序
- 实体类集合(List<Object>)根据实体类的一个或者多个属性对集合进行排序-Collections.sort
- *单例集合、泛型
- 往新建的泛型集合List<T>中添加对象
- C#基础-059 泛型集合List<>与字典Dictionary<,>
- Java 泛型 和 集合的嵌套
- C#读取数据库返回泛型集合 把DataSet类型转换为List<T>泛型集合
- java使用lambda表达式对List集合进行操作技巧(JDK1.8)
- 职场招聘:我如何进行简历的筛选与人员的选择
- Java--集合(LinkedList,泛型)
- C# List泛型集合