C # Linq Order By operation

Order By operation

Applicable scene : a query statement to sort out, such as sorting by time and so on.

Description : Specifies the collection sort expression; delay: the collection specified sort expression; delay, the default is ascending, descending together for descending, the corresponding extension and is OrderBy OrderByDescending

1. Simple form

This example uses orderby by hire date of the employee to sort:

var q = from e in db.Employees orderby e.HireDate select e;

Description: The default is ascending

2. conditional form

Note: Where and Order By order does not matter. Whereas in T-SQL, Where, and Order By strict position limit.

var q = from o in db.Orders where o.ShipCity == "London" orderby o.Freight select o;

Statement Description: Use the where and orderby sort by shipping.

3. Sort Descending

var q = from p in db.Products orderby p.UnitPrice descending select p;

4.ThenBy

Statement Description: Use multiple orderby customers sort sort:

var q = from c in db.Customers orderby c.City, c.ContactName select c;

Description: sort by a plurality of expressions, for example, press City sort, when the same City, sorted by ContactName. This sentence written Lambda expressions like this:

var q = db.Customers .OrderBy(c => c.City) .ThenBy(c => c.ContactName).ToList();

Not in T-SQL ThenBy statement, it still translated into OrderBy, so it can be expressed with the following statement:

var q = db.Customers .OrderBy(c => c.ContactName) .OrderBy(c => c.City).ToList();

Is to be noted that, when the plurality of operation OrderBy, cascading method is in reverse order. For descending, can be replaced by the corresponding descending operator.

var q = db.Customers .OrderByDescending(c => c.City) .ThenByDescending(c => c.ContactName).ToList();

It should be noted, OrderBy operation, does not support sorting by type, does not support anonymous classes. such as

var q = db.Customers .OrderBy(c => new { c.City, c.ContactName }).ToList();

It will be thrown. There is a mistake in front of the anonymous class operation, when talk OrderBy, comparison is category. such as

var q = db.Customers .Select(c => new     { c.City, c.Address }) .OrderBy(c => c).ToList();

If you want to use OrderBy (c => c), with the proviso that, in the previous step, the generated categories as an object to be basic types C # language. For example, the next sentence, where City to string type.

var q = db.Customers .Select(c => c.City) .OrderBy(c => c).ToList();

5.ThenByDescending

Both are used in extended mode after OrderBy / OrderByDescending, the first ThenBy / ThenByDescending extension method as the second sort, the second ThenBy / ThenByDescending as the third sort, and so on

var q = from o in db.Orders where o.EmployeeID == 1 orderby o.ShipCountry, o.Freight descending select o;

Statement Description: Using orderby sent to the press in descending order of countries, then shipping on orders EmployeeID 1 sort.

6. With GroupBy form

var q = from p in db.Products group p by p.CategoryID into g orderby g.Key select new { g.Key, MostExpensiveProducts = from p2 in g where p2.UnitPrice == g.Max(p3 => p3.UnitPrice) select p2 };

Statement Description: Using orderby, Max and Group By derived the highest unit price for each product category, product group and press CategoryID this sort.

Guess you like

Origin blog.csdn.net/cxu123321/article/details/92574712