Order by Operation
Scenario: Sort the query out of the statement, such as by time and so on.
Description: Sorts the collection by the specified expression; delay: Sorts the collection by the specified expression; delay, which is ascending by default, plus descending for descending, and the corresponding extension method is Order by and OrderByDescending
1. Simple Form
This example uses order-by to sort employees by hire date:
var q =
From E in DB. Employees
E.hiredate
Select E;
Description: The default is ascending
2. With conditional form
Note: The order of Where and order by is not important. In T-SQL, where and order by have strict location restrictions.
var q =
From O in Db. Orders
where o.shipcity = = "London"
O.freight
Select O;
Statement Description: Use where and order by to sort by freight.
3. Descending sort
var q =
from P in Db. Products
P.unitprice Descending
Select P;
4.ThenBy
Statement Description: Sort the customer by using a composite order by order:
var q =
From C in DB. Customers
C.city, C.contactname
Select C;
Description: Sort by multiple expressions, such as by City first, and when City is sorted by ContactName. This sentence is written in a lambda expression like this:
var q =
. (c = c.city)
. ThenBy (c = c.contactname). ToList ();
There is no ThenBy statement in T-SQL, which is still translated as an order by, so you can also use the following statement to express:
var q =
Db. Customers
. (c = c.contactname)
. (c = c.city). ToList ();
It is important to note that when multiple order-by operations are made, the cascade is in reverse order. For descending, replace with the corresponding descending operator.
var q =
Db. Customers
. OrderByDescending (c = c.city)
. ThenByDescending (c = c.contactname). ToList ();
It is necessary to note that the order by type is not supported, and anonymous classes are not supported. Like what
var q =
Db. Customers
. (c = new
{
C.city,
C.contactname
}). ToList ();
will be thrown out of the exception. The error is that the preceding operation has an anonymous class, and then, when followed by, the category is compared. Like what
var q =
Db. Customers
. Select (c = new
{
C.city,
C.address
})
. (c = c). ToList ();
If you want to use the order order (c + + c), the precondition is that the category of the resulting object must be the basic type of the C # language in the preceding step. For example, here City is a string type.
var q =
Db. Customers
. Select (c = c.city)
. (c = c). ToList ();
5.ThenByDescending
Both of these extension methods are used after orderby/orderbydescending, the first thenby/thenbydescending extension method as the second sort basis, and the second thenby/ ThenByDescending is used as a third-digit sort basis, and so on
var q =
From O in Db. Orders
where O.employeeid = = 1
O.shipcountry, O.freight Descending
Select O;
Statement Description: Order orders for EmployeeID 1 are ordered first by sending to the country and then by freight from high to low.
6. With GroupBy form
var q =
from P in Db. Products
Group p by P.categoryid into G
G.key
Select New {
G.key,
Mostexpensiveproducts =
From P2 in G
where P2. UnitPrice = = G.max (P3 = p3. UnitPrice)
Select P2
};
Statement Description: Use ORDER BY, Max, and group by to derive the highest unit price in each category and sort the products by CategoryID.