LINQ Notes

11-1: Some of the Visual Basic clauses for working with LINQ

Clause Description
From Identifies the source of data for the query.
Where Provides a condition that specifies which elements are retrieved from the data source.
Order By Indicates how the elements that are returned by the query are sorted.
Select Specifies the content of the returned elements.
Let Performs a calculation and assigns an alias to the result that can then be used within the query.
Join Combines data from two data sources.
Aggregate Lets you include aggregate functions, such as Sum, Average, and Count, in a query.
Group By Groups the returned elements and, optionally, applies aggregate functions to each group.

Advantages of using LINQ

  • Makes it easier for you to query a data source by integrating the query language with Visual Basic.
  • Makes it easier to develop applications that query a data source by providing IntelliSense, compile-time syntax checking, and debugging support.
  • Makes it easier for you to query different types of data sources because you use the same basic syntax for each type.
  • Makes it easier for you to use objects to work with relational data sources by providing designer tools that create object-relational mappings.

Description

  • Language-Integrated Query (LINQ) provides a set of query operators that are implemented using extension methods. These methods are static members of the Enumerable and Queryable classes.
  • You can work with LINQ by calling the extension methods directly or by using Visual Basic clauses that are converted to calls to the methods at compile time.
  • A query that calls LINQ methods directly is called a method-based query. A query that uses Visual Basic clauses is called a query expression. You use a method-based query or query expression to identify the data you want to retrieve from a data source.
  • To use LINQ with a data source, the data source must implement the IEnumerable(Of T) interface or another interface that implements IEnumerable(Of T) such as IQueryable(Of T). A data source that implements one of these interfaces is called an enumerable type.

Finally, if you’re working with a relational data source such as a SQL Server database, you can use designer tools provided by Visual Studio to develop an object-relational mapping. Then, you can use LINQ to query the objects defined by this mapping, and the query will be converted to the form required by the data source. This can make it significantly easier to work with relational data sources.

Visual Basic 2008 features that support LINQ

Visual Basic 2008 introduced a variety of new features to support LINQ. These features are listed in the first table in figure 11-2. Most of these features can be used outside of LINQ. For the most part, though, you’ll use these features when you code LINQ queries. You’ll learn more about these features later in this chapter.

LINQ providers included with Visual Basic 2008

Figure 11-2 also presents the LINQ providers that are included with Visual Basic 2008. As I’ve already mentioned, you’ll learn how to use the LINQ to Objects provider in this chapter to query generic lists. Then, in chapter 12, you’ll learn how to use the LINQ to DataSet provider to query the data in a dataset. In chapter 13, you’ll learn how to use the Object Relational Designer to create an object model for use with the LINQ to SQL provider, and you’ll learn how to use that model to query a SQL Server database. Then, in chapter 14, you’ll learn how to update the data in a SQL Server database using LINQ to SQL with an object model. In chapter 16, you’ll learn how to use the LINQ to XML provider to load XML from a file, query and modify the XML in your application, and save the updated XML to a file. You’ll also learn how to create XML documents and elements from scratch or from other documents and elements.

Another provider you can use with Visual Basic is LINQ to Entities. This provider works with an Entity Data Model that maps the data in a relational database to the objects used by your application. In chapter 17, you’ll learn how to use the Entity Data Model Designer to create an Entity Data Model. Then, in chapter 18, you’ll learn how to use LINQ to Entities to work with this model.

Visual Basic 2008 features that support LINQ

Feature Description
Query expressions Expressions with a syntax similar to SQL that can be used to retrieve and update data. Converted into method calls at compile time.
Implicitly typed variables Variables whose types are inferred from the data that’s assigned to them. Used frequently in query expressions and with query variables.
Anonymous types An unnamed type that’s created temporarily when a query returns selected fields from the data source.
Object initializers Used with query expressions that return anonymous types to assign values to the properties of the anonymous type.
Extension methods Provide for adding methods to a data type from outside the definition of the data type.

LINQ providers included with Visual Basic 2008

Provider Description
LINQ to Objects Lets you query in-memory data structures such as generic lists and arrays. LINQ to DataSet Lets you query the data in a typed or untyped dataset. See chapter 12 for more information.
LINQ to SQL Lets you query and update the data in a SQL Server database. See chapters 13 and 14 for more information.
LINQ to XML Lets you query and modify in-memory XML or the XML stored in a file. See chapter 16 for more information.
LINQ to Entities Lets you query and update the data in any relational database. See chapter 18 for more information.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: