An Introduction to LINQ - VB.NET
(Page 2 of 4 )
And in VB.NET, we can represent this as follows:
Class Person
Private _name As String
Private _age As Integer
Private _phone As String
Public Sub New(ByVal name As String, ByVal age As Integer, ByVal phone As String)
_name = name
_age = age
_phone = phone
End Sub
Public ReadOnly Property Name() As String
Get
Return _name
End Get
End Property
Public ReadOnly Property Age() As Integer
Get
Return _age
End Get
End Property
Public ReadOnly Property Phone() As String
Get
Return _phone
End Get
End Property
End Class
Now, let's say that we have various Person objects, all arranged in an array:
Person bob = new Person("Bob", 35, "555-292-3044");
Person henry = new Person("Henry", 43, "555-292-5312");
Person joe = new Person("Joe", 22, "555-232-7222");
Person chuck = new Person("Chuck", 29, "555-292-1134");
Person[] people = { bob, henry, joe, chuck };
Dim bob As Person = New Person("Bob", 35, "555-292-3044")
Dim henry As Person = New Person("Henry", 43, "555-292-5312")
Dim joe As Person = New Person("Joe", 22, "555-232-7222")
Dim chuck As Person = New Person("Chuck", 29, "555-292-1134")
Dim people() As Person = {bob, henry, joe, chuck}
Suppose we wanted to extract a subset of our array. For example, we can create a collection containing only people over the age of thirty. To do this, we could loop over the array, checking each element's Age property. This approach, however, is made unnecessary by LINQ and, furthermore, is ugly by comparison. So, let's get started with LINQ. Here is how to query the array, obtaining all elements whose Age property is more than thirty:
IEnumerable<Person> overThirty = from p in people
where p.Age > 30
select p;
Dim overThirty As IEnumerable(Of Person) = From p In people _
Where p.Age > 30 _
Select p
Above, we simply look through each Person object, represented as p, in the array and select, or pull out, each element whose Age property is over thirty. To do this, we use the From, Where and Select operators. From identifies where we're looking, Where sets the conditions we're testing, and Select pulls out the results in the form we want (here, we just pull out the results as Person objects).
The resulting code using LINQ is, obviously, much more concise and elegant than the alternative, and it's a lot more readable as well. From a casual glance, one can tell exactly what's being done.
Note that the query syntax is just shorthand for the following:
IEnumerable<Person> overThirty = people
.Where(p => p.Age > 30)
.Select(p => p);
Dim overThirty As IEnumerable(Of Person) = people _
.Where(Function(p) p.Age > 30) _
.Select(Function(p) p)
Above, the operators are translated into methods. For each operator, we need a function that will perform the relevant task. For example, with Where, we need a function that will determine if p.Age is more than thirty. Lambda expressions are used to provide functions here. They are similar to anonymous functions and were added to support LINQ.
Next: Implicitly Typed Variables and Anonymous Types >>
More .NET Articles
More By Peyton McCullough