【发布时间】:2014-01-29 02:42:08
【问题描述】:
我想对Student 对象列表应用过滤器。我找到了三种方法来做到这一点:
第一道
使用FindAll
List<Student> liste = Admin.GetStudentList().FindAll(x => x.Age > 20);
第二种方式
使用yield关键字的方法
public List<Student> GetStudentListByAge(int age){
foreach(Student s in Admin.GetStudentList()){
if(s.Age > age) yield return s;
}
}
第三条路
使用本地列表:
public List<Student> GetStudentListByAge(int age){
List<Student> list2 = new List<Student>();
foreach(Student s in Admin.GetStudentList()){
if(s.Age > age) list2.Add(s);
}
return list2;
}
所以,我需要知道它之间的最佳方式是什么?为什么?在哪些情况下?
【问题讨论】:
-
为什么不使用来自 LINQ 的
Where? -
是的,这是另一种可能性。所以我需要哪个是最好的
-
最好怎么做?最快的?最易读?最不可能失败?您的要求是什么?您是否进行过任何测试和试验,以确定哪一种最适合您?
-
@Default : 我需要知道哪个是最快的,即如果列表的元素数量很大,哪个是最快给出结果的?
标签: c# .net oop collections yield