linq-sort ORDER BY

Source: Internet
Author: User
Tags sorts

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 =     from inch 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 =     from inch db. Orders    where= =  "London" by    o.freightselect o;

Statement Description: Use where and order by to sort by freight.

3. Descending sort
var =      from inch db. Products by    p.unitprice descending    select p;
4.ThenBy

Statement Description: Sort the customer by using a composite order by order:

var =     from inch db. Customers    c.city,    c.contactnameselect 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 = = c.city )            = 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 =      db. Customers    =  c.contactname)    = 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 =      db. Customers    =  c.city)    = 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 =      db. Customers    =  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 =      db. Customers    . Select = New    {        c.city,        c.address    })    = + 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 =      db. Customers    . Select = c.city)     = + 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 =     from inch db. Orders    where= =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
varQ=      fromPinchdb. ProductsGroupP byP.categoryid intoG. G.Key    Selectnew {G.Key, Mostexpensiveproducts=             fromP2inchgwhereP2. UnitPrice==G.Max(P3=P3. UnitPrice)SelectP2};

Statement Description: Use ORDER BY, Max, and group by to derive the highest unit price in each category and sort the products by CategoryID.

linq-sort ORDER BY

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.