我的任务是创建一个类Student
并创建一个方法,仅当学生的名字按字母顺序使用 LINQ 查询运算符在他们的姓氏之前,才从数组中挑选学生。我写了这个Student
类:
public class Student
{
private string firstName;
private string familyName;
private int age;
public Student(string firstName, string familyName, int age)
: this()
{
this.firstName = firstName;
this.familyName = familyName;
this.age = age;
}
public Student()
{
firstName = "Dancho";
familyName = "Mastikata";
age = 24;
this.firstName = firstName;
this.familyName = familyName;
this.age = age;
}
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string FamilyName
{
get { return familyName; }
set { familyName = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
}
然后我编码了这个:
public class StudentsManipulations
{
static void Main()
{
Student[] students = new Student[6]{new Student("Georgi", "Milanov", 21 ),
new Student("Ilko", "Pirgov", 30 ),
new Student("Neboisha", "Yelenkovich", 34),
new Student("Dimitar", "Berbatov", 32 ),
new Student( ),
new Student("Nikolai", "Bodurov", 24 )};
}
public static List<Student> StudentSortByFirstName(Student[] students)
{
List<Student> sortedStudents = from student in students
where student.FirstName.CompareTo(student.FamilyName) < 0
select student;
return sortedStudents;
}
}
不幸的是 where 关键字有一个错误,我无法完全理解:
无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“System.Collections.Generic.List”。存在显式转换(您是否缺少演员表?)。
你能帮我理解到底是什么问题吗?