Source Selection Using a from Clause

Query expressions start by the use of a from clause, which has two tasks. First, it refers to the source that’s getting queried, such as an in-memory collection or a reference to an object representing a table in a database. Second, it establishes a name for the items in that source, which is used subsequently to express filters, orderings, and so on. The following is an example:

var nums = new List<int> { 1, 2, 3, 4, 5 };from n in nums ...

Observe a few things here. On the second line, I’ve not assigned the query expression to a variable to emphasize it’s an expression. You can use the whole query expression as a parameter to a method, assign it to a variable, or whatever else ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.