Home

LINQ and Arrays

     

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 Shared Function ToArray(Of TSource) ( _
    source As IEnumerable(Of TSource) _
) As TSource()

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:

Imports System.Linq
Imports System.Collections.Generic

Public Enum Genders
    Female
    Male
    Unknown
End Enum

Public Class Employee
    Public EmployeeNumber As Integer
    Public FirstName As String
    Public LastName As String
    Public Gender As Genders
    Public HourlySalary As Double

    Public Sub New(Optional ByVal Number As Integer = 0,
                   Optional ByVal FName As String = "John",
                   Optional ByVal LName As String = "Doe",
                   Optional ByVal Gdr As Genders = Genders.Unknown,
                   Optional ByVal salary As Double = 0D)
        EmployeeNumber = Number
        FirstName = FName
        LastName = LName
        Gender = Gdr
        HourlySalary = salary
    End Sub
End Class

Module Exercise
    Public Function Main() As Integer
        Dim Employees() As Employee =
        {
            New Employee(971974, "Patricia", "Katts", Genders.Female, 24.68),
            New Employee(408415, "Leslie", "Simms", Genders.Unknown, 15.72),
            New Employee(208411, "Raymond", "Kouma", Genders.Male, 20.15),
            New Employee(279374, "H�l�ne", "Mukoko", Genders.Female, 15.55),
            New Employee(607575, "Hermine", "Kensley", Genders.Female, 12.55),
            New Employee(707912, "Bertrand", "Yamaguchi", Genders.Male, 24.68),
            New Employee(475022, "Leslie", "Simms", Genders.Unknown, 15.72),
            New Employee(448660, "Helios", "Panko", Genders.Unknown, 12.69),
            New Employee(294800, "Peter", "Mukoko", Genders.Male, 18.85),
            New Employee(971394, "Gertrude", "Monay", Genders.Female, 20.55),
            New Employee(680404, "Lydia", "Kensley", Genders.Female, 22.58)
        }

        Dim Empls As IEnumerable(Of Employee) = From StaffMembers
                                                In Employees
                                                Select StaffMembers
        Dim Contractors() As Employee = Empls.ToArray()

        Console.WriteLine("+========+============+===========+=========+========+")
        Console.WriteLine("| Empl # | First Name | Last Name | Gender  | Salary |")
        Console.WriteLine("+========+============+===========+=========+========+")
        For Each Staff In Contractors
            Console.WriteLine("| {0,6} | {1,-10} | {2,-9} | {3,-7} | {4,6} |",
                              Staff.EmployeeNumber, Staff.FirstName, Staff.LastName,
                              Staff.Gender, Staff.HourlySalary)
            Console.WriteLine("+--------+------------+-----------+---------+--------+")
        Next

        Console.WriteLine()
        Return 0
    End Function
End Module

This would produce:

+========+============+===========+=========+========+
| Empl # | First Name | Last Name | Gender  | Salary |
+========+============+===========+=========+========+
| 971974 | Patricia   | Katts     | Female  |  24.68 |
+--------+------------+-----------+---------+--------+
| 408415 | Leslie     | Simms     | Unknown |  15.72 |
+--------+------------+-----------+---------+--------+
| 208411 | Raymond    | Kouma     | Male    |  20.15 |
+--------+------------+-----------+---------+--------+
| 279374 | H�l�ne     | Mukoko    | Female  |  15.55 |
+--------+------------+-----------+---------+--------+
| 607575 | Hermine    | Kensley   | Female  |  12.55 |
+--------+------------+-----------+---------+--------+
| 707912 | Bertrand   | Yamaguchi | Male    |  24.68 |
+--------+------------+-----------+---------+--------+
| 475022 | Leslie     | Simms     | Unknown |  15.72 |
+--------+------------+-----------+---------+--------+
| 448660 | Helios     | Panko     | Unknown |  12.69 |
+--------+------------+-----------+---------+--------+
| 294800 | Peter      | Mukoko    | Male    |  18.85 |
+--------+------------+-----------+---------+--------+
| 971394 | Gertrude   | Monay     | Female  |  20.55 |
+--------+------------+-----------+---------+--------+
| 680404 | Lydia      | Kensley   | Female  |  22.58 |
+--------+------------+-----------+---------+--------+

Press any key to continue . . .

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:

Imports System.Linq
Imports System.Collections.Generic

Public Enum Genders
    Female
    Male
    Unknown
End Enum

Public Class Employee
    Public EmployeeNumber As Integer
    Public FirstName As String
    Public LastName As String
    Public Gender As Genders
    Public HourlySalary As Double

    Public Sub New(Optional ByVal Number As Integer = 0,
                   Optional ByVal FName As String = "John",
                   Optional ByVal LName As String = "Doe",
                   Optional ByVal Gdr As Genders = Genders.Unknown,
                   Optional ByVal salary As Double = 0D)
        EmployeeNumber = Number
        FirstName = FName
        LastName = LName
        Gender = Gdr
        HourlySalary = salary
    End Sub
End Class

Module Exercise
    Public Function Main() As Integer
        Dim Employees() As Employee =
        {
            New Employee(971974, "Patricia", "Katts", Genders.Female, 24.68),
            New Employee(408415, "Leslie", "Simms", Genders.Unknown, 15.72),
            New Employee(208411, "Raymond", "Kouma", Genders.Male, 20.15),
            New Employee(279374, "H�l�ne", "Mukoko", Genders.Female, 15.55),
            New Employee(607575, "Hermine", "Kensley", Genders.Female, 12.55),
            New Employee(707912, "Bertrand", "Yamaguchi", Genders.Male, 24.68),
            New Employee(475022, "Leslie", "Simms", Genders.Unknown, 15.72),
            New Employee(448660, "Helios", "Panko", Genders.Unknown, 12.69),
            New Employee(294800, "Peter", "Mukoko", Genders.Male, 18.85),
            New Employee(971394, "Gertrude", "Monay", Genders.Female, 20.55),
            New Employee(680404, "Lydia", "Kensley", Genders.Female, 22.58)
        }

        Dim Empls As IEnumerable(Of Employee) = (From StaffMembers
                                                In Employees
                                                Select StaffMembers).ToArray()

        Console.WriteLine("+========+============+===========+=========+========+")
        Console.WriteLine("| Empl # | First Name | Last Name | Gender  | Salary |")
        Console.WriteLine("+========+============+===========+=========+========+")
        For Each Staff In Empls
            Console.WriteLine("| {0,6} | {1,-10} | {2,-9} | {3,-7} | {4,6} |",
                              Staff.EmployeeNumber, Staff.FirstName, Staff.LastName,
                              Staff.Gender, Staff.HourlySalary)
            Console.WriteLine("+--------+------------+-----------+---------+--------+")
        Next


        Console.WriteLine()
        Return 0
    End Function
End Module

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 Shared Function ToList(Of TSource) ( _
    source As IEnumerable(Of TSource) _
) As List(Of TSource)

This method follows the same rules as its counterpart the ToArray() method except that you must declare a List(Of ...) generic variable for it. Here is an example:

Imports System.Linq
Imports System.Collections.Generic

Public Enum Genders
    Female
    Male
    Unknown
End Enum

Public Class Employee
    Public EmployeeNumber As Integer
    Public FirstName As String
    Public LastName As String
    Public Gender As Genders
    Public HourlySalary As Double

    Public Sub New(Optional ByVal Number As Integer = 0,
                   Optional ByVal FName As String = "John",
                   Optional ByVal LName As String = "Doe",
                   Optional ByVal Gdr As Genders = Genders.Unknown,
                   Optional ByVal salary As Double = 0D)
        EmployeeNumber = Number
        FirstName = FName
        LastName = LName
        Gender = Gdr
        HourlySalary = salary
    End Sub
End Class

Module Exercise
    Public Function Main() As Integer
        Dim Employees() As Employee =
        {
            New Employee(971974, "Patricia", "Katts", Genders.Female, 24.68),
            New Employee(408415, "Leslie", "Simms", Genders.Unknown, 15.72),
            New Employee(208411, "Raymond", "Kouma", Genders.Male, 20.15),
            New Employee(279374, "H�l�ne", "Mukoko", Genders.Female, 15.55),
            New Employee(607575, "Hermine", "Kensley", Genders.Female, 12.55),
            New Employee(707912, "Bertrand", "Yamaguchi", Genders.Male, 24.68),
            New Employee(475022, "Leslie", "Simms", Genders.Unknown, 15.72),
            New Employee(448660, "Helios", "Panko", Genders.Unknown, 12.69),
            New Employee(294800, "Peter", "Mukoko", Genders.Male, 18.85),
            New Employee(971394, "Gertrude", "Monay", Genders.Female, 20.55),
            New Employee(680404, "Lydia", "Kensley", Genders.Female, 22.58)
        }

        Dim Empls As IEnumerable(Of Employee) = From StaffMembers
                                                In Employees
                                                Select StaffMembers

        Dim Contractors As List(Of Employee) = Empls.ToList()

        Console.WriteLine("+========+============+===========+=========+========+")
        Console.WriteLine("| Empl # | First Name | Last Name | Gender  | Salary |")
        Console.WriteLine("+========+============+===========+=========+========+")
        For Each Staff In Contractors
            Console.WriteLine("| {0,6} | {1,-10} | {2,-9} | {3,-7} | {4,6} |",
                              Staff.EmployeeNumber, Staff.FirstName, Staff.LastName,
                              Staff.Gender, Staff.HourlySalary)
            Console.WriteLine("+--------+------------+-----------+---------+--------+")
        Next


        Console.WriteLine()
        Return 0
    End Function
End Module

You can then use the List(Of ...) variable as you see fit.

 
 
     
 

Previous Copyright © 2010-2016, FunctionX Next