LINQ standard query operators (i)--select, SelectMany, Where, to-do, OrderByDescending, ThenBy, thenbydescending, and reverse

Source: Internet
Author: User

This article from: http://blog.csdn.net/xuejianwu/article/details/6931804

One, the projection operator 1. Select

The Select operator projects the values in a single sequence or collection. In the following example, use Select to return all the columns of the employee table from the sequence:

[CSharp]View Plaincopy
  1. using (NorthwindDataContext db=New NorthwindDataContext ())
  2. {
  3. //query syntax
  4. var query =
  5. From E in db. Employees
  6. where E.firstname.startswith ("M")
  7. Select E;
  8. //Method Syntax
  9. var q =
  10. Db. Employees
  11. . Where (E = E.firstname.startswith ("M"))
  12. . Select (e = e);
  13. foreach (var item in query)
  14. {
  15. Console.WriteLine (item. FirstName);
  16. }
  17. }


Of course, you can also return a single column, for example:

[CSharp]View Plaincopy
    1. var query =
    2. From E in db. Employees
    3. where E.firstname.startswith ("M")
    4. Select E.firstname;

You can also return a few columns in a sequence, for example:

[CSharp]View Plaincopy
    1. var query =
    2. From E in db. Employees
    3. where E.firstname.startswith ("M")
    4. Select New
    5. {
    6. E.firstname,
    7. E.lastname,
    8. E.title
    9. };

2. SelectMany

The SelectMany operator provides the ability to combine multiple from clauses, which merges the results of each object into a single sequence. Here is an example:

[CSharp]View Plaincopy
  1. using (NorthwindDataContext db=New NorthwindDataContext ())
  2. {
  3. //query syntax
  4. var query =
  5. From E in db. Employees
  6. From O in e.orders
  7. Select O;
  8. //Method Syntax
  9. var q =
  10. Db. Employees
  11. . SelectMany (e = e.orders);
  12. foreach (var item in query)
  13. {
  14. Console.WriteLine (item. Freight);
  15. }
  16. }

Second, the restriction operator

Where is the restriction operator, which applies the filtering criteria to the sequence and filters the data in the sequence according to the provided logic.

The where operator does not start execution of the query. When you start a time-lapse query on a sequence to begin execution, the filter is applied to the query. The use of the Where operator has already appeared in the first section and is no longer redundant.

Third, sort operators

Sorting operators, including order BY, OrderByDescending, ThenBy, ThenByDescending, and reverse, provide ascending or descending sorting.

1.

The order by operator sorts the elements in the sequence in ascending. The following example demonstrates this:

[CSharp]View Plaincopy
  1. using (NorthwindDataContext db = new NorthwindDataContext ())
  2. {
  3. //query syntax
  4. var query =
  5. From E in db. Employees
  6. E.firstname
  7. Select E;
  8. //Method Syntax
  9. var q =
  10. Db. Employees
  11. . (E = e.firstname)
  12. . Select (e = e);
  13. foreach (var item in q)
  14. {
  15. Console.WriteLine (item. FirstName);
  16. }
  17. }

Here you can specify how the sequence is ordered by using the overloaded method order (func<t,tkey>,icomparer<tkey>).

2. OrderByDescending

The orderbydescending operator arranges the elements in a sequence in descending order. The usage is the same as the by, no longer demonstrated here.

3. ThenBy

The ThenBy operator implements ascending order of sequences by sub-keyword. The query syntax for this operator is slightly different from the method syntax, as shown in the following code:

[CSharp]View Plaincopy
  1. using (NorthwindDataContext db = new NorthwindDataContext ())
  2. {
  3. //query syntax
  4. var query =
  5. From E in db. Employees
  6. E.firstname,e.lastname
  7. Select E;
  8. //Method Syntax
  9. var q =
  10. Db. Employees
  11. . (E = e.firstname)
  12. . ThenBy (e = e.lastname)
  13. . Select (e = e);
  14. foreach (var item in query)
  15. {
  16. Console.WriteLine (item. FirstName);
  17. }
  18. }

4. ThenByDescending

The thenbydescending operator implements descending order of sequences by the secondary keyword. The query syntax for this operator is slightly different from the method syntax, as shown in the following code:

[CSharp]View Plaincopy
  1. using (NorthwindDataContext db = new NorthwindDataContext ())
  2. {
  3. //query syntax
  4. var query =
  5. From E in db. Employees
  6. E.firstname,e.lastname Descending
  7. Select E;
  8. //Method Syntax
  9. var q =
  10. Db. Employees
  11. . (E = e.firstname)
  12. . ThenByDescending (e = e.lastname)
  13. . Select (e = e);
  14. foreach (var item in query)
  15. {
  16. Console.WriteLine (item. FirstName);
  17. }
  18. }

5. Reverse

The reverse will reverse the elements in the sequence sequentially from the back to the front. It is important to note that the return value of the reverse method is void, as shown in the following code:

[CSharp]View Plaincopy
    1. using (NorthwindDataContext db = new NorthwindDataContext ())
    2. {
    3. //Method Syntax
    4. var q =
    5. Db. Employees
    6. . Select (e = e.firstname)
    7. . ToList ();
    8. Q.reverse ();
    9. foreach (var item in q)
    10. {
    11. Console.WriteLine (item);
    12. }
    13. }

LINQ standard query operators (i)--select, SelectMany, Where, to-do, OrderByDescending, ThenBy, thenbydescending, and reverse

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.