7.2. LINQing with From, Where, and Select

LINQ adds a bunch of keywords to Visual Basic. If you've worked with SQL statements, you'll recognize many of the keywords, especially From, Select, and Where. This section shows how these popular clauses work, including samples of their syntax.

NOTE

In a LINQ query, you declare a variable to hold the return value (for example, Dim q), but you don't have to declare what type of variable it is (such as String or Boolean). LINQ uses anonymous types because it often deals with data that contains multiple types that aren't compatible. When LINQ can't tell you what type a query returns, LINQ just makes up a type and passes it off as anonymous.

In Visual Basic, a LINQ expression must appear as one statement; thus, one line can become very long. To break the line and improve readability, Visual Basic supports a space followed by an underscore character (_) as you see in the examples.

7.2.1. Targeting the source in a From...In clause

A From clause points to the source collection (data or objects) and declares an iteration variable. You use an iteration variable as a temporary representative for one item in a collection of items. In the following example, tz is the iteration variable, and System.TimeZoneInfo.GetSystemTimeZones returns the collection:

Dim q = From tz In System.TimeZoneInfo.GetSystemTimeZones

The preceding statement goes In the ...

Get ASP.NET 3.5 For Dummies® now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.