2013-03-12 222 views
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; 
    } 
}    

不幸的是,在關鍵字的錯誤,我不能完全理解:

無法將類型 'System.Collections.Generic.IEnumerable'隱式轉換爲 'System.Collections.Generic.List'。存在明確轉換 (您是否缺少演員?)。

你能幫我理解究竟是什麼錯誤嗎?

+3

我喜歡你的偉大的BG足球運動員名單:) – EkoostikMartin 2013-03-12 13:29:59

+0

感謝您的幫助 - 每個人! – 2013-03-12 16:45:32

回答

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(); 
+0

這是一個匿名類型嗎? – 2013-03-12 13:30:34

+0

不,'var'表示一個隱式類型的局部變量。閱讀此參考資料:http://msdn.microsoft.com/en-us/library/bb383973.aspx – MarcinJuraszek 2013-03-12 13:31:49

1

你應該在你的聲明的末尾添加.ToList()

這種轉換的可枚舉到一個列表

2

你只需要您的LINQ表達式轉換到一個列表:

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

你的錯誤意味着你的流利的LINQ是返回一個IEnumerable<Student>,而不是List<Student>你期望。您需要撥打另外的電話.ToList(),或者您可以使用IEnumerable<Student>

1

添加.ToList()到您的查詢:

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

這個查詢,

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

返回IEnumerable<Student>

如果你想有一個List<Student>,那麼你應該對所得到的IEnumerable<Student>撥打ToList()

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

注意,作爲一般規則,LINQ查詢進行評估時,結果被枚舉,而不是之前(這稱爲「懶惰執行」或「延遲加載」)。然而,調用ToList()將立即執行查詢,枚舉結果並填充列表。

相關問題