O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

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

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 ...

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