O'Reilly logo

Visual Basic 2012 Programmer's Reference by Rod Stephens

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

BASIC LINQ QUERY SYNTAX

The following text shows the typical syntax for a LINQ query:

From ... Where ... Order By ... Select ...

The following sections describe these four basic clauses. The sections after those describe some of the other most useful LINQ clauses.

From

The From clause is the only one that is required. It tells where the data comes from and defines the name by which it is known within the LINQ query. Its basic form is:

From query_variable In data_source

Here query_variable is a variable that you are declaring to manipulate the items selected from the data_source. This is similar to declaring a looping variable in a For or For Each statement.

You can supply a data type for query_variable if you know its type, although because of the anonymous types used by LINQ, it’s often easiest to let LINQ infer the data type automatically. For example, the following query explicitly indicates that the query variable cust is from the Customer class:

Dim query = From cust As Customer In all_customers

The From clause can include more than one query variable and data source. In that case, the query selects data from all of the data sources. For example, the following query selects objects from the all_customers and all_orders lists:

Dim query = From cust In all_customers, ord In all_orders

This query returns the cross-product of the objects in the two lists. In other words, for every object in the all_customers list, the query returns that object paired with every object in the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required