我有两节课。第一个是Person,第二个是Student(继承自 Person)。我想过滤一个通用列表并找到所有学生的成绩高于 7。我想出了以下解决方案:
class Person
{
public string Name {get; set;}
}
class Student : Person
{
public decimal Grade {get; set;}
}
class Program
{
static void Main(string[] args)
{
List<Person> people = new List<Person>();
people.Add(new Person() {Name="John"});
people.Add(new Student() {Name="Joe", Grade=6});
people.Add(new Student() {Name="Jane", Grade=8});
people.Where(delegate (Person person) {
var student = person as Student;
return student != null && student.Grade > 7;
});
}
}
有没有更简单的方法来过滤这个列表?