Reputation: 63
I have a task to create a class Student
and to create a method that pick the Students from an array only if their first name is before their last name alphabetically using LINQ query operators. I've written the Student
class:
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; }
}
}
And then I coded this one:
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;
}
}
Unfortunately there is an error in where keyword, that I can't completely understand:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?).
Can you please help me understand what exactly is wrong?
Upvotes: 0
Views: 1108
Reputation: 77304
Your error means that your fluent Linq is returning an IEnumerable<Student>
, not the List<Student>
you expect. You will need an additional .ToList()
call or you can use the IEnumerable<Student>
instead.
Upvotes: 3
Reputation: 125630
Use ToList()
method to enumerate results and put them into a list:
var query = from student in students
where student.FirstName.CompareTo(student.FamilyName) < 0
select student;
List<Student> sortedStudents = query.ToList();
Or in one statement:
List<Student> sortedStudents = (from student in students
where student.FirstName.CompareTo(student.FamilyName) < 0
select student).ToList();
Upvotes: 9
Reputation: 116468
This query,
from student in students
where student.FirstName.CompareTo(student.FamilyName) < 0
select student
returns an IEnumerable<Student>
.
If you want a List<Student>
, then you should call ToList()
on the resulting IEnumerable<Student>
:
List<Student> sortedStudents =
(from student in students
where student.FirstName.CompareTo(student.FamilyName) < 0
select student)
.ToList();
Note that as a general rule, LINQ queries are evaluated when the result is enumerated, and not before (this is called "lazy execution" or "lazy loading"). Calling ToList()
however will execute the query immediately, enumerate the results, and populate a list.
Upvotes: 2
Reputation: 14618
Add .ToList() to your query:
List<Student> sortedStudents = (from student in students where student.FirstName.CompareTo(student.FamilyName) < 0 select student).Tolist();
Upvotes: 1
Reputation: 6911
You just need to convert your linq expression to a list:
List<Student> sortedStudents = (from student in students
where student.FirstName.CompareTo(student.FamilyName) < 0 select student).ToList();
Upvotes: 2
Reputation: 2629
You should add .ToList()
at the end of your statement.
This converts your Enumerable to a List
Upvotes: 1