LINQ expressions are built from query clauses that have the same feel as clauses in SQL statements at the database level. With the exception of the
From clause, which must appear first, the other clauses can generally appear in any order within the query.
Every basic LINQ query begins with the
Dim bookBag =
Frombk In Library ShowResults(bookBag) ' Results --> War and Peace LT 1424 ' Anna Karenina LT 976 ' Ben-Hur LW 544 ' Peter Pan JB 192
This four-word query is pretty much the shortest LINQ query you can write. I stored the results of the query in the
bookBag variable (with its data type inferred by the query), but the query can also be used directly as an expression.
ShowResults(From bk In Library)
bk variable included in the query is known as a range variable or iteration variable. (You don't have to use "bk"; I just chose that name at random. It's a variable, so give it any name you wish.) This variable provides a way to identify objects and object members from the source data within the query. Since
Library is a collection, it wouldn't make sense to say
Library.Title when referring to the title of just one book. Instead, you refer to
Personally, I find this
source syntax a little indirect. I much prefer the table-alias syntax used in SQL queries.
SELECT * FROM Library
AS keyword performs much the same function as the LINQ
In keyword. Yet despite my internal tension, the
In syntax prevails; ...