INTRODUCTION TO LINQ

The LINQ API provides relatively low-level access to data in various storage formats. Visual Basic provides a higher-level layer above the LINQ API that makes querying data sources easier. This higher-level layer uses query expressions to define the data that should be selected from a data source. These expressions use a SQL-like syntax so they will be familiar to developers who have worked with relational databases.

For example, suppose a program defines a Customer class that provides typical customer properties such as Name, Phone, StreetAddress, AccountBalance, and so forth. Suppose also that the list all_customers holds all of the application’s Customer objects. Then the following expression defines a query that selects customers with negative account balances. The results are ordered by balance in ascending order so customers with the most negative balances (who owe the most) are listed first. (Example program LinqLambda, which is available for download on the book’s website, defines a simple Customer class and performs a similar query.)

Dim overdue_custs =
    From cust In all_customers
    Where cust.AccountBalance < 0
    Order By cust.AccountBalance Ascending
    Select cust.Name, cust.AccountBalance

Behind the scenes, Visual Basic transforms the query expression into calls to the LINQ API and fetches the selected data. The program can then loop through the results as shown in the following code:

For Each cust In overdue_custs
    Debug.WriteLine(cust.Name & ": " & cust.AccountBalance)
Next cust

There are a couple of interesting things to note about this code. First, the previous code fragments do not declare data types for the overdue_custs expression or the looping variable cust in the For Each loop. The data types for both of these variables are inferred automatically by Visual Basic. If you stop the program while it is executing and use the TypeName function to see what types these variables have, you’ll find that they have the following ungainly names:

WhereSelectEnumerableIterator'2
VB$AnonymousType_0'2

Because these data types have such awkward names, you don’t really want to try to guess them. It’s much easier to leave Option Infer on and let Visual Basic infer them for you.

In fact, as the previous code fragments show, you never even need to know what these data types are. The code can define the query without declaring its types, and the For Each loop can iterate through the results without knowing the data type of the looping variable.

Because the code doesn’t need to know what these data types really are, they are called anonymous types.

A second interesting fact about this code is that the program doesn’t actually fetch any data when the query expression is defined. It only accesses the data source (in this case the all_customers list) when the code tries to access the result in the For Each loop. Many programs don’t really need to distinguish between when the expression is declared and when it is executed. For example, if the code iterates through the results right after defining the query, there isn’t much difference. However, if it may be a long time between defining the query and using it or if the query takes a long time to execute, the difference may matter.

Third, if you have any experience with relational databases, you’ll notice that the Select clause is in a different position from where it would be in a SQL statement. In SQL the Select clause comes first whereas in LINQ it comes at the end. This placement is due to implementation issues Microsoft encountered while implementing IntelliSense for LINQ. The concept is similar in SQL and LINQ. In both cases the Select clause tells which “fields” you want to select from the data. As long as you remember the difference in position (or let IntelliSense help you remember), it shouldn’t be too confusing.


INTELLISENSE DEFERRED
Basically IntelliSense doesn’t know what “fields” you can select until it knows what fields are available. In the preceding example, the From clause indicates that the data will be selected from all_customers, a list of Customer objects. It isn’t until after the From clause that IntelliSense knows that the Select statement can pick from the Customer class’s properties.

The following sections explain the most useful LINQ keywords that are supported by Visual Basic.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.246.247