0

我的任务是创建一个类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”。存在显式转换(您是否缺少演员表?)。

你能帮我理解到底是什么问题吗?

4

6 回答 6

9

使用ToList()方法枚举结果并将它们放入列表中:

var query = from student in students
            where student.FirstName.CompareTo(student.FamilyName) < 0
            select student;

List<Student> sortedStudents = query.ToList();

或者在一个声明中:

List<Student> sortedStudents = (from student in students
                                where student.FirstName.CompareTo(student.FamilyName) < 0
                                select student).ToList();
于 2013-03-12T13:28:56.800 回答
3

您的错误意味着您的流利的 Linq 正在返回一个IEnumerable<Student>,而不是List<Student>您期望的。您将需要一个额外的.ToList()电话,或者您可以使用IEnumerable<Student>

于 2013-03-12T13:29:44.897 回答
2

这个查询,

from student in students 
where student.FirstName.CompareTo(student.FamilyName) < 0 
select student

返回一个IEnumerable<Student>.

如果你想要一个List<Student>,那么你应该调用ToList()结果IEnumerable<Student>

List<Student> sortedStudents = 
    (from student in students 
     where student.FirstName.CompareTo(student.FamilyName) < 0 
     select student)
    .ToList();

请注意,作为一般规则,LINQ 查询是在枚举结果时评估的,而不是之前(这称为“延迟执行”或“延迟加载”)。然而,调用ToList()将立即执行查询、枚举结果并填充列表。

于 2013-03-12T13:30:51.153 回答
2

您只需要将 linq 表达式转换为列表:

List<Student> sortedStudents = (from student in students 
where student.FirstName.CompareTo(student.FamilyName) < 0 select student).ToList();
于 2013-03-12T13:29:13.490 回答
1

.ToList()添加到您的查询中:

List<Student> sortedStudents = (from student in students where student.FirstName.CompareTo(student.FamilyName) < 0 select student).Tolist();
于 2013-03-12T13:30:12.417 回答
1

您应该.ToList()在声明的末尾添加。

这会将您的 Enumerable 转换为 List

于 2013-03-12T13:29:04.763 回答