
ith the release of Visual Studio 2008, Microsoft has also updated the VB language to the latest version, 9.0. VB 9.0 boasts several key language enhancements that have been made to support the new Language Integrated Query (LINQ) feature that Microsoft has announced earlier. Part 2 of this series picks up where
Part 1 left off, walking you through VB 9.0's new LINQ support features and providing code examples that illustrate how to use them.
LINQ
One of the key additions to the .NET Framework v3.5 is the support for Language Integrated Query (LINQ). LINQ allows you to manipulate data just like you manipulate database recordsusing a query language, like SQL. So, whereas before you had to write complex iterative loops to retrieve data, you can now just specify data declaratively using LINQ and the compiler does all the work for you.
LINQ to Objects
LINQ to Objects allows you to use LINQ to query any IEnumerable or IEnumerable(Of t) collections directly. For example, suppose you have a collection of data stored in an array and you want to retrieve a subset of the data quickly. In the old days, you'd write a loop and iteratively retrieve all the data matching your criteria. This is time-consuming because you'd have to write all the logic to perform the comparison, etc. Using LINQ, you can declaratively write the condition using an SQL-like statement and the compiler will do the job for you.
Let's use a real-life example. Suppose you have an array of type String that contains a list of names:
Dim AllNames As String() = _
{"Jeffrey", "Kirby", "Gabriel", "Philip", "Ross", "Adam", _
"Alston", "Warren"}
Now suppose you need to print out all the names in this array that start with the letter "G." In this case, you can set up a loop and iteratively perform a comparison on each name. Things start to get more complex when you have more complicated filtering rules. Using LINQ, you could specify the filter using the
From clause, like this:
Dim FoundNames As IEnumerable(Of String) = _
From Name In AllNames Where Name.StartsWith("G")
When this statement has been executed,
FoundNames will now contain a collection of names that starts with the letter "G." It this case, it returns "Gabriel." You can now add the names to a ListBox control:
For Each PeopleName In FoundNames
ListBox1.Items.Add(PeopleName)
Next
You can also use a more complex filter:
Dim FoundNames As IEnumerable(Of String) = _
From Name In AllNames Where Name.StartsWith("G") Or _
Name.Contains("by") Order By Name
In this case,
FoundNames will now contain "Gabriel, Kirby." Note that the result is also sorted.
Another useful application of LINQ is to manipulate Windows Forms controls. Suppose you have a large number of controls on your form and you want to uncheck all the CheckBox controls without setting each one individually. You can use the following code with LINQ:
'---retrieve all the checkbox controls in the current form---
Dim checkboxes As IEnumerable = _
From ctrl In Me.Controls Where TypeOf ctrl Is CheckBox
For Each c As CheckBox In checkboxes
c.Checked = False
Next