Introduction to LINQ — Book Excerpt

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

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 first section, “Introduction to LINQ,” provides an intuitive introduction to LINQ. Many of the details about LINQ functions are so complex and technical that they can be hard to understand, but the basic ideas are really quite simple. The introduction gives examples that demonstrate the essential concepts to try to give you an understanding of the basics.

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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS