description | title | ms.date | f1_keywords | helpviewer_keywords | ms.assetid | ||||
---|---|---|---|---|---|---|---|---|---|
from clause - C# Reference |
from clause |
07/20/2015 |
|
|
1aefd18c-1314-47f8-99ec-9bcefb09e699 |
A query expression must begin with a from
clause. Additionally, a query expression can contain sub-queries, which also begin with a from
clause. The from
clause specifies the following:
-
The data source on which the query or sub-query will be run.
-
A local range variable that represents each element in the source sequence.
Both the range variable and the data source are strongly typed. The data source referenced in the from
clause must have a type of xref:System.Collections.IEnumerable, xref:System.Collections.Generic.IEnumerable%601, or a derived type such as xref:System.Linq.IQueryable%601.
In the following example, numbers
is the data source and num
is the range variable. Note that both variables are strongly typed even though the var keyword is used.
[!code-csharpcscsrefQueryKeywords#1]
The compiler infers the type of the range variable when the data source implements xref:System.Collections.Generic.IEnumerable%601. For example, if the source has a type of IEnumerable<Customer>
, then the range variable is inferred to be Customer
. The only time that you must specify the type explicitly is when the source is a non-generic IEnumerable
type such as xref:System.Collections.ArrayList. For more information, see How to query an ArrayList with LINQ.
In the previous example num
is inferred to be of type int
. Because the range variable is strongly typed, you can call methods on it or use it in other operations. For example, instead of writing select num
, you could write select num.ToString()
to cause the query expression to return a sequence of strings instead of integers. Or you could write select num + 10
to cause the expression to return the sequence 14, 11, 13, 12, 10. For more information, see select clause.
The range variable is like an iteration variable in a foreach statement except for one very important difference: a range variable never actually stores data from the source. It's just a syntactic convenience that enables the query to describe what will occur when the query is executed. For more information, see Introduction to LINQ Queries (C#).
In some cases, each element in the source sequence may itself be either a sequence or contain a sequence. For example, your data source may be an IEnumerable<Student>
where each student object in the sequence contains a list of test scores. To access the inner list within each Student
element, you can use compound from
clauses. The technique is like using nested foreach statements. You can add where or orderby clauses to either from
clause to filter the results. The following example shows a sequence of Student
objects, each of which contains an inner List
of integers representing test scores. To access the inner list, use a compound from
clause. You can insert clauses between the two from
clauses if necessary.
[!code-csharpcscsrefQueryKeywords#2]
A compound from
clause is used to access inner collections in a single data source. However, a query can also contain multiple from
clauses that generate supplemental queries from independent data sources. This technique enables you to perform certain types of join operations that are not possible by using the join clause.
The following example shows how two from
clauses can be used to form a complete cross join of two data sources.
[!code-csharpcscsrefQueryKeywords#3]
For more information about join operations that use multiple from
clauses, see Perform left outer joins.