One of the new technologies introduced with LINQ is called LINQ query expressions. It's also called "syntactic sugar" because it's really just a faster and easier way to write statements that can be written other ways. (Some programmers almost seem to believe that code should be hard to write, not easier. Visual Basic has always been all about "faster and easier".)
To get started, consider an array created by this code (the quote is from Thomas Paine's The Crisis):
Dim Words As String = "These are the times that try men's souls!"
Dim WordArray() As String = Split(Words)
This creates a collection that can easily be processed with the familiar and useful For loop:
Console.WriteLine("Complete Array (The Old Way)")
For i = 0 To UBound(WordArray)
Console.WriteLine(WordArray(i))
Next
VB.NET offers another way to process the array - the For-Each loop. To use this technique a group has to "implement the IEnumerable interface of the System.Collections namespace or the IEnumerable interface of the System.Collections.Generic namespace." (From MSDN) Our array qualifies, so we can write:
Console.WriteLine(vbNewLine & "Complete Array (For Each)")
For Each Word As String In WordArray
Console.WriteLine(Word)
Next
But what if you only want use some of the elements in the collection? For example, all of the words shorter than four characters. You could embed an If statement inside the loop:
Console.WriteLine(vbNewLine & "Conditional For Each")
For Each Word As String In WordArray
If Word.Length < 4 Then Console.WriteLine(Word)
Next
The LINQ technology offers still yet another way to do it, however. A LINQ query that does the same thing can be written using the traditional ".dot notation" to code the new LINQ query methods .Where and .Select:
Console.WriteLine(vbNewLine & "Dot Notation")
Dim Sequence As IEnumerable(Of String)
Sequence = _
WordArray _
.Where(Function(n) n.Length < 4) _
.Select(Function(n) n)
For Each Name As String In Sequence
Console.WriteLine(Name)
Next
At it's heart, LINQ is just another set of methods and properties that you can use. Where and Select are just new methods. While the "old" If statement looks a lot easier in this simple example ... (Let's be honest. It is a lot easier in this case.) ... LINQ opens the door to a whole new world where collections can be processed in a single statement with the compiler doing the work. For example, you could sort the array just as easily.
Sequence = WordArray.OrderBy(Function(n) n)
Which gives you:
are
men's
souls!
that
the
These
times
try
(An interesting question worthy of Thomas Paine himself, actually.)
And if the "Function(n) n" syntax looks little unnecessary and confusing, it's because we're just scratching the surface of what it can do. Note that one reason the LINQ example takes more code to write is that LINQ creates another collection: Sequence.
But Microsoft recognized that a lot of programmers might like to use a syntax that is familiar, comfortable, and most of all, very successful: SQL. So they added syntax to Visual Basic that lets you do the same thing, but use syntax that looks a lot like you're writing a SQL query (I used a different method, "Contains", just for variety):
Console.WriteLine(vbNewLine & "Query Expression")
Sequence = _
From n In WordArray _
Where n.Contains("e") _
Select n
For Each Name As String In Sequence
Console.WriteLine(Name)
Next
The takeaway is that LINQ queries are really a lot easier than you might have thought!
