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
- using (NorthwindDataContext db=New NorthwindDataContext ())
- {
- //query syntax
- var query =
- From E in db. Employees
- where E.firstname.startswith ("M")
- Select E;
- //Method Syntax
- var q =
- Db. Employees
- . Where (E = E.firstname.startswith ("M"))
- . Select (e = e);
- foreach (var item in query)
- {
- Console.WriteLine (item. FirstName);
- }
- }
Of course, you can also return a single column, for example:
[CSharp]View Plaincopy
- var query =
- From E in db. Employees
- where E.firstname.startswith ("M")
- Select E.firstname;
You can also return a few columns in a sequence, for example:
[CSharp]View Plaincopy
- var query =
- From E in db. Employees
- where E.firstname.startswith ("M")
- Select New
- {
- E.firstname,
- E.lastname,
- E.title
- };
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
- using (NorthwindDataContext db=New NorthwindDataContext ())
- {
- //query syntax
- var query =
- From E in db. Employees
- From O in e.orders
- Select O;
- //Method Syntax
- var q =
- Db. Employees
- . SelectMany (e = e.orders);
- foreach (var item in query)
- {
- Console.WriteLine (item. Freight);
- }
- }
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
- using (NorthwindDataContext db = new NorthwindDataContext ())
- {
- //query syntax
- var query =
- From E in db. Employees
- E.firstname
- Select E;
- //Method Syntax
- var q =
- Db. Employees
- . (E = e.firstname)
- . Select (e = e);
- foreach (var item in q)
- {
- Console.WriteLine (item. FirstName);
- }
- }
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
- using (NorthwindDataContext db = new NorthwindDataContext ())
- {
- //query syntax
- var query =
- From E in db. Employees
- E.firstname,e.lastname
- Select E;
- //Method Syntax
- var q =
- Db. Employees
- . (E = e.firstname)
- . ThenBy (e = e.lastname)
- . Select (e = e);
- foreach (var item in query)
- {
- Console.WriteLine (item. FirstName);
- }
- }
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
- using (NorthwindDataContext db = new NorthwindDataContext ())
- {
- //query syntax
- var query =
- From E in db. Employees
- E.firstname,e.lastname Descending
- Select E;
- //Method Syntax
- var q =
- Db. Employees
- . (E = e.firstname)
- . ThenByDescending (e = e.lastname)
- . Select (e = e);
- foreach (var item in query)
- {
- Console.WriteLine (item. FirstName);
- }
- }
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
- using (NorthwindDataContext db = new NorthwindDataContext ())
- {
- //Method Syntax
- var q =
- Db. Employees
- . Select (e = e.firstname)
- . ToList ();
- Q.reverse ();
- foreach (var item in q)
- {
- Console.WriteLine (item);
- }
- }
LINQ standard query operators (i)--select, SelectMany, Where, to-do, OrderByDescending, ThenBy, thenbydescending, and reverse