How LINQ works with in memory collections in VB.NET

Web developer uses in this case the simplest form of LINQ – LINQ to Objects.  Let for example Web developer has some sort of data class, like the Clients class shown next:

 

Public Class Client

Private _ClientID As Integer

Private _FirstName As String

Private _LastName As String

Private _TitleOfCourtesy As String

 

Public Sub Client(ByVal CliID As Integer, ByVal FName As String, _

ByVal LName As String, ByVal Title As String)

ClientID = CliID

FirstName = FName

LastName = LName

TitleOfCourtesy = Title

End Sub

Public Property ClientID() As Integer

Get

Return _ClientID

End Get

Set(ByVal value As Integer)

_ClientID = value

End Set

End Property

Public Property FirstName() As String

Get

Return _FirstName

End Get

Set(ByVal value As String)

_FirstName = value

End Set

End Property

Public Property LastName() As String

Get

Return _LastName

End Get

Set(ByVal value As String)

_LastName = value

End Set

End Property

Public Property TitleOfCourtesy() As String

Get

Return _TitleOfCourtesy

End Get

Set(ByVal value As String)

_TitleOfCourtesy = value

End Set

End Property

End Class

 

Web developer can easily create collection that consists of Client objects, like the strongly typed List shown:

‘ Create the collection.

Dim clients As List(Of Client) = New List(Of Client)()

‘ Fill the collection.

clients.Add(New Client With {.ClientID = 100, .FirstName = “Brian”, .LastName = “Shapp”, .TitleOfCourtesy = “Mr.”})

 

clients.Add(New Client With {.ClientID = 200, .FirstName = “Emilie”, .LastName = “Heman”, .TitleOfCourtesy = “Ms.”})

 

clients.Add(New Client With {.ClientID = 200, .FirstName = “Robertt”, .LastName = “Elliott”, .TitleOfCourtesy = “Dr.”})

 

The traditional approach to get a list of all clients who have a last name that starts with the letter D is to use code to loop through the full collection of clients and add each matching client to a second collection:

 

‘ Create the collection.

Dim clients As List(Of Client) = New List(Of Client)()

‘ Find the matching clients.

Dim matches As New ArrayList()

Dim client As Client

For Each client In clients

If client.LastName.EndsWith(“D”) Then

matches.Add(client)

End If

Next

‘ Display the collection of the matches in a web page

gridClients.DataSource = matches

gridClients.DataBind()

Web developer can use LINQ to Objects to replace iterative logic ( For Each block ) with a  declarative expression. The following example shows how Web developer can rewrite the earlier example, replacing the foreach block with a LINQ expression that queries the collection:

 

‘ Create the collection.

Dim clients As List(Of Client) = New List(Of Client)()

‘ (Code for filling the collection omitted to save space.)

Dim matches = From client In clients _

Where client.LastName.StartsWith(“D”)

Select client

‘ Display the collection of the matches in a web page

gridClients.DataSource = matches

gridClients.DataBind()

The new things here are:

-The LINQ expression uses a set of new keywords, including from, in, where, and select. Web developer shapes his/her query using these keywords.

-LINQ expressions return an usual type of object, called an iterator object. (In the example, the iterator object is named matches.)  Although the iterator object looks like an ordinary collection, it doesn’t actually hold any information. Instead, it has the ability to fetch the data when it is necessary. Wen Web developer examines the contents of an iterator object with a foreach block or when he/she binds it to a control, LINQ evaluates his/her expression and quickly grabs the information is needed. This is called deferred execution.

-In the example the iterator object (named matches) is defined with the var keyword This is a shortcut that tells the VB.NET compiler to use the correct data type, without forcing Web developer to specify it. Technically, the iterator object could be one of several different types of objects depending on the clauses used in in the LINQ expression. All of these objects implement the strongly typed version of the IEnumerable interface and Web developer can replace the var with IEnumerable<Client>, because the collection holds Client objects.