Type relationships in LINQ query operations

Source: Internet
Author: User
Tags foreach anonymous range

Type relationships in Query operations (LINQ)

To write a query effectively, you should understand how the variable types in the complete query operation are all related to each other. If you understand these relationships, you can make it easier to understand the LINQ samples and code examples in your document. In addition, you can understand the background operation when you use VAR implicitly to type a variable.

LINQ query operations are strongly typed in the data source, the query itself, and query execution. The type of the variable in the query must be compatible with the type of the element in the data source and the type of the iteration variable in the foreach statement. This strong type guarantees that type errors are caught at compile time so that they can be corrected before the user encounters these errors.

To demonstrate these type relationships, most of the following examples use an explicit type for all variables. The last example shows how to apply the same principle when you take advantage of an implicit type that uses Var.

Queries that do not convert source data

The following illustration shows a LINQ to Objects query operation that does not convert data. The source contains a sequence of strings, and the query output is also a string sequence.

1, the type parameter of the data source determines the type of the range variable.

2, the type of the selected object determines the type of query variable. The name here is a string. Therefore, the query variable is a IEnumerable.

3. Iterate through the query variable in the foreach statement. Because the query variable is a sequence of strings, the iteration variable is also a string.

To convert a query for source data

The following illustration shows a LINQ to SQL query operation that performs a simple transformation of data. The query uses a sequence of Customer objects as input and selects only the Name attribute in the result. Because Name is a string, the query generates a string sequence as output.

1, the type parameter of the data source determines the type of the range variable.

2, the SELECT statement returns the Name property, not the complete Customer object. Because Name is a string, the Custnamequery type argument is string rather than Customer.

3. Because Custnamequery is a string sequence, the iteration variable of the Foreach loop must also be a string.

The following illustration shows a slightly more complex transformation. The SELECT statement returns an anonymous type that captures only the two members of the original Customer object.

1, the type parameter of the data source is always the type of the range variable in the query.

2. Because the SELECT statement generates an anonymous type, you must implicitly type the query variable with var.

3. Because the type of the query variable is implicit, the iteration variable in the Foreach loop must also be implicit.

Let the compiler infer type information

Although you should understand the type relationships in a query operation, you can also choose to have the compiler perform all of your work for you. Keyword var can be used for any local variable in a query operation. The following figure is exactly equivalent to the second example discussed earlier. The only difference is that the compiler will provide a strong type for each variable in the query operation:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.