The following is an excerpt from Chapter 21 of “Visual Basic 2010 Programmer’s Reference.”
This comprehensive tutorial and reference guide provides programmers and developers of all skill and experience levels with a broad, solid understanding of essential Visual Basic 2010 topics and clearly explains how to use this powerful programming language to perform a variety of tasks. As a tutorial, the book describes the Visual Basic language and covers essential Visual Basic topics. Also serving as a reference guide, the material presents categorized information regarding specific operations and reveals useful tips and tricks to help you make the most of Visual Basic 2010.
Chapter 21, “LINQ,” appeals to intermediate level developers and managers who know the basics but haven’t mastered LINQ’s oddities.
[login] LINQ (Language Integrated Query, pronounced “link”) is a data selection mechanism designed to give programs the ability to select data in the same way from any data source. Ideally the program would be able to use exactly the same method to fetch data whether it’s stored in arrays, lists, relational databases, XML data, Excel worksheets, or some other data store. Currently the LINQ API supports data stored in relational databases, objects within the program stored in arrays or lists, and XML data.
This chapter only covers the default LINQ providers included with Visual Basic, but you can build providers to make LINQ work with just about anything. For a list of some third party LINQ providers to Google, Amazon, Excel, Active Directory, and more, see rshelton.com/archive/2008/07/11/list-of-linq-providers.aspx.
LINQ is a complex topic. LINQ provides dozens of extension methods that apply to all sorts of objects that hold data such as arrays, dictionaries, and lists. Visual Basic provides a LINQ query syntax that converts SQL-like queries into calls to LINQ functions.
LINQ tools are divided into the three categories summarized in the following list:
- * LINQ to Objects refers to LINQ functions that interact with Visual Basic objects such as arrays, dictionaries, and lists. Most of this chapter presents examples using these kinds of objects to demonstrate LINQ concepts.
* LINQ to XML refers to LINQ features that read and write XML data. Using LINQ, you can easily move data between XML hierarchies and other Visual Basic objects.
* LINQ to ADO.NET refers to LINQ features that let you write LINQ-style queries to extract data from relational databases.
The section “Basic LINQ Query Syntax” describes the most useful LINQ query commands. These let you perform complex queries that select, filter, and arrange data taken from program objects. The next section, “Advanced LINQ Query Syntax,” describes additional LINQ query commands.
“LINQ Functions” describes functions that are provided by LINQ but that are not supported by Visual Basic’s LINQ query syntax. To use these functions, you must apply them to the arrays, dictionaries, lists, and other objects that they extend.
“LINQ Extension Methods” explains how LINQ extends objects such as arrays, dictionaries, and lists. It describes method-based queries and explains how you can write your own extensions to increase the power of method-based queries.
After describing the tools provided by LINQ, most of the rest of the chapter describes the three main categories of LINQ usage: LINQ to Objects, LINQ to XML, and LINQ to ADO.NET. The chapter finishes by describing Parallel LINQ (PLINQ).
LINQ to Objects is a bit easier to cover effectively than LINQ to XML and LINQ to ADO.NET because it doesn’t require that you have any special knowledge beyond Visual Basic itself. To understand LINQ to XML properly, you need to understand XML, which is a complex topic in its own right. Similarly, to get the most out of LINQ to ADO.NET, you need to understand relational databases such as SQL Server, a huge topic about which many books have been written.
Because LINQ to Objects is easiest to cover, this chapter focuses mostly on it, and most of the examples throughout the chapter deal with LINQ to Objects. The final sections of the chapter do provide some information about LINQ to XML and LINQ to ADO.NET, however, to give you an idea of what is possible in those arenas.
Reproduced from Visual Basic 2010 Programmer’s Reference by permission of Wiley. Copyright 2010. All rights reserved.