The authors of the Apress book Pro LINQ state that:
"Virtually every significant enhancement to the VB.NET language in version 2008 was made specifically to support LINQ."
I've written separate articles about some of these enhancements:
-> Lamda Expressions
-> LINQ Queries
-> Partial Methods
And my introduction to VB.NET for Beginners ... Visual Basic .NET 2008 Express - A "From the Ground Up" Tutorial ... is fully updated with LINQ examples.
LINQ is such a fundamental topic in VB.NET - and it's so widely misunderstood - that I decided the About Visual Basic site needed a more structured and complete introduction in one place. This is the introduction to a series to explain and demonstrate LINQ in a more structured way. The goal is for a beginning Visual Basic .NET programmer to get up to speed on LINQ.
One thing that can be confusing is that as time goes on, the name LINQ is being used less and less, mainly because it's so thoroughly just a part of .NET now. When it was first introduced, Microsoft made a big marketing push with the name LINQ just to generate some excitement. Now, many of the features that are part of LINQ are really just ".NET" and nobody thinks of them as a separate technology anymore.
The place to start is the name "LINQ". The expansion is "Language INtegrated Query" but that doesn't begin to do it justice because it's really so much more. LINQ can be used to create and filter data into arrays, enumerable classes, XML, relational databases, and other data stores. You can even use LINQ query expressions as the basis for statements like building event handlers. This is one of the reasons LINQ can be confusing. It's like the elephant and the blind men fable. Your understanding of LINQ will depend a lot on the application of LINQ that you happen to learn about first.
But since we do have to start somewhere, a very simple query is probably the way to go. Here's one:
Dim articles =
From a In pnlArticles.Controls
Where a.checked = True
Select a
For Each article In articles
Process.Start("IExplore.exe", article.tag)
Next
Only setting the Tag property of each CheckBox control to the URL string of the web page is left out of the example shown above. The illustration below shows this starting app in action:
--------
Click Here to display the illustration
--------
Leaving aside the fascinating question of why advertisers think my site is a good place to sell vegetarian food and interest people in learning more about Mormonism, LINQ gives you a really flexible way to start any or all of a list of web pages. The actual LINQ code is completely contained in the Dim statement assignment clause:
From ...
Where ...
Select ...
In brief, LINQ adds the abililty to use the powerful query syntax that has made SQL such a rockstar hit inside VB.NET programs. The key word here is "inside". LINQ is "language level" querying. It all happens as part of your program source and that gives you a whole bunch of advantages:
-> Intellisense to speed coding
-> Compile time errors rather than having your program crash at runtime.
-> The ability to use the same syntax with different data sources
One reason LINQ can be so tough to get a handle on is that it's really a lot of things and it's not obvious where to start learning. This series breaks LINQ into two parts, customized for VB.NET programmers:
-> VB.NET Language Enhancements for LINQ
-> Data Sources for LINQ
VB.NET Language Enhancements for LINQ
As the opening quote from Pro LINQ states, there are quite a few really fundamental new features in VB.NET in Framework 3.5/VB.NET 2008 specifically for LINQ:
-> New VB.NET Options
-> Lambda Expressions
-> Expression Trees
-> The Enhanced Dim Keyword, which includes:
--> Object Initializers
--> Anonymous Types
-> Extension Methods
-> Partial Methods
-> Query Expressions
-> Nullable Types
-> XML Enhancements
Whew! That's quite a list!
Data Sources for LINQ
To wrap your mind around the LINQ data sources, you mainly have to understand ...
-> What categories of data will work with LINQ
-> What query operators will work in LINQ
The various "flavors" of LINQ that you see at Microsoft are just these different categories of data accessible to the LINQ query syntax. If the URL's of these About Visual Basic articles were in an XML dataset instead of the Tag properties of controls, the code would look about the same:
Dim articles =
From a In Articles.Elements("URL")
Where CStr(a.Element("checked")) = "True"
Select a.Element("address")
This cross data compatibility is probably the main reason you should learn LINQ. You might be forgiven for not learning TSQL since it can only be used if you have a SQL Server database (depending on your employer, of course) but LINQ can be used everywhere.
The categories of data that will work with LINQ are the divisions you see at Microsoft:
-> SQL Server databases: LINQ to SQL
-> XML documents: LINQ to XML
-> ADO.NET Datasets: LINQ to DataSet
-> Entity Framework: LINQ to Entities
-> .NET collections, files, strings and so on: LINQ to Objects
... and the list is still expanding. Third party companies are supplying their own LINQ software that allow you to use the same LINQ syntax. (LINQ to XYZAccounting? LINQ to AcmeGraphicsImages?) Within Microsoft, any collection of objects that supports IEnumerable or the generic IEnumerable(Of T) interface will work with LINQ.
The query operators supported by LINQ are:
-> Where
-> Select/SelectMany
-> Take/Skip/ TakeWhile/SkipWhile
-> Join/GroupJoin
-> Concat
-> OrderBy/ThenBy/OrderByDescending/ThenByDescending
-> GroupBy
-> Distinct
-> Union/Intersect
-> Except
-> AsEnumerable
-> ToArray/ToList
-> ToDictionary/ToLookup
-> OfType/Cast
-> SequenceEqual
-> First/FirstOrDefault/Last/LastOrDefault/Single/SingleOrDefault
-> ElementAt/ElementAtOrDefault
-> DefaultIfEmpty
-> Range
-> Repeat
-> Empty
-> Any/All
-> Contains
-> Count/LongCount
-> Sum/Min/Max/Average
-> Aggregate
I've broken with tradition and quoted this list from Microsoft documentation mainly because it's not likely to change. If a new operator was added or removed from the list, it would break backward compatibility for LINQ. Microsoft is likely to think long and hard before they did that. But LINQ syntax for new types of data are likely to be added from this point on.
The final part of LINQ is the language extensions that were necessary to really make it work. These include:
Implicitly typed variables
Notice that in the simple example at the top, I used the syntax ...
Dim articles =
From a In pnlArticles.Controls
I didn't have to write ...
Dim articles As ControlCollectionIn fact, trying to code it that way adds a lot of syntactic complexity. (Try it!) LINQ makes it easy to just let the compiler figure it out but the variable articles is still strongly typed. (And that's a good thing!)
Anonymous types
Like implicit types, anonymous types just let the compiler figure it out. In the Select earlier, the result is a CheckBox object, but I didn't have to code that. This syntax would also work:
From a As CheckBox In pnlArticles.Controls
Object Initializer
It might not be obvious, but the object a was created and initialized behind the scenes. Before LINQ, this was something you would have to do. It isn't really important for you to understand anything except that it works.
Lambda expressions
This one is important to understand, but it can also be a puzzler, which is why it's going to be left for a later article in this series. But in brief, a Lambda expression lets you write the code "in line" as part of the query. The bottom line, as a beginner, is that it makes things easier, not harder ... once you figure out what they do.

