Converting a LINQ Result to an Array or List
|
A LINQ expression produces a list of values and the resulting list can be stored in a variable created in a from clause. If you want, you can store the result of the LINQ expression in a specific (or external) variable. To support this, the IEnumerable interface provides the ToArray() and the ToList() methods. The syntax of the ToArray() method is: |
public static TSource[] ToArray<TSource>( this IEnumerable<TSource> source;)
To store the results of an IEnumerable list in an
array, declare the array variable and assign the resulting list to it.
Here is an example:
Here is an example:
using System;
using System.Linq;
using System.Collections.Generic;
public class Exercise
{
public static int Main()
{
var students = new Student[]
{
new Student(82495, "Carlton", "Blanchard"),
new Student(20935, "Charlotte", "O'Keefe"),
new Student(79274, "Christine", "Burns"),
new Student(79204, "Edward", "Swanson"),
new Student(92804, "Charles", "Pressmann")
};
IEnumerable<Student> pupils = from studs
in students
select studs;
Student[] attendees = pupils.ToArray();
Console.WriteLine("+=======+============+===========+");
Console.WriteLine("| Std # | First Name | Last Name |");
foreach (var std in attendees)
{
Console.WriteLine("+-------+------------+-----------+");
Console.WriteLine("| {0,5} | {1,-10} | {2,-9} |", std.StudentNumber,
std.FirstName, std.LastName);
}
Console.WriteLine("+======+============+============+");
Console.WriteLine();
return 0;
}
}
public class Student
{
public int StudentNumber;
public string FirstName;
public string LastName;
public Student(int number = 0,
string firstName = "John",
string lastName = "Doe")
{
StudentNumber = number;
FirstName = firstName;
LastName = lastName;
}
}
This would produce:
Instead of first creating the select list, then declaring an array variable, you can include the LINQ statement in parentheses and call the ToArray() method outside the closing parenthesis. Here is an example:
public class Exercise { public static int Main() { var students = new Student[] { new Student(82495, "Carlton", "Blanchard"), new Student(20935, "Charlotte", "O'Keefe"), new Student(79274, "Christine", "Burns"), new Student(79204, "Edward", "Swanson"), new Student(92804, "Charles", "Pressmann") }; Student[] pupils = (from studs in students // orderby studs.LastName // where studs.Gender == Genders.Female select studs).ToArray(); Console.WriteLine("+=======+============+===========+"); Console.WriteLine("| Std # | First Name | Last Name |"); foreach (var std in pupils) { Console.WriteLine("+-------+------------+-----------+"); Console.WriteLine("| {0,5} | {1,-10} | {2,-9} |", std.StudentNumber, std.FirstName, std.LastName); } Console.WriteLine("+======+============+============+"); Console.WriteLine(); return 0; } }
Instead of storing the result of a LINQ statement into an array, you can store it in a collection. To support this, the IEnumerable interface is equipped with the ToList() method.
Its syntax is:
public static List<TSource> ToList<TSource>( this IEnumerable<TSource> source);
This method follows the same rules as its counterpart
the ToArray() method except that you must declare a List<>
generic variable for it.
Here is an example:
Here is an example:
public class Exercise
{
public static int Main()
{
var students = new Student[]
{
new Student(82495, "Carlton", "Blanchard"),
new Student(20935, "Charlotte", "O'Keefe"),
new Student(79274, "Christine", "Burns"),
new Student(79204, "Edward", "Swanson"),
new Student(92804, "Charles", "Pressmann")
};
IEnumerable<Student> pupils = from studs
in students
// orderby studs.LastName
// where studs.Gender == Genders.Female
select studs;
List<Student> attendees = pupils.ToList();
Console.WriteLine("+=======+============+===========+");
Console.WriteLine("| Std # | First Name | Last Name |");
foreach (var std in attendees)
{
Console.WriteLine("+-------+------------+-----------+");
Console.WriteLine("| {0,5} | {1,-10} | {2,-9} |", std.StudentNumber,
std.FirstName, std.LastName);
}
Console.WriteLine("+======+============+============+");
Console.WriteLine();
return 0;
}
}
You can then use the List<> variable as you see fit.
No comments :
Post a Comment