C #3.0 entry series (6)-orderby operations

Source: Internet
Author: User
This section describes the orderby operation. I suddenly think of such a problem, the reader will T-SQL? If I don't know, is it too easy for me to write? Do a survey. I don't know how to raise my hand.
Orderby operation
Simple, sorted by employment date, in ascending order by default

VaR q =
From E in db. Employees
Orderby E. hiredate
Select E;

Shipcity is London. VaR q =
From o in db. Orders
Where o. shipcity = "London"
Orderby O. Freight
Select O;
Or
VaR q =
From o in db. Orders
Orderby O. Freight
Where o. shipcity = "London"
Select O;

Here the order of where and orderby is not important. In T-SQL, where and orderby have strict location restrictions.
Orderbydescending, in descending order of price. VaR q =
From P in db. Products
Orderby P. unitprice descending
Select P;

Thenby and thenbydescending, that is, sort by multiple columns. The first column is sorted by city, city, and contactname. In the second example, the second sequence is descending. Thenby:
VaR q =
From C in db. MERs
Orderby C. City, C. contactname
Select C;

Thenbydescending:
VaR q =
From o in db. Orders
Where o. employeeid = 1
Orderby O. shipcountry, O. Freight descending
Select O;

Explain the two sentences. For the thenby operation, the Cascade form is:
VaR q = dB. Customers. orderby (C => C. City). thenby (C => C. contactname). tolist ();
Because there is no thenby statement in the T-SQL, it is still translated as orderby
Therefore, you can also use the following statement to express
VaR q = dB. Customers. orderby (C => C. contactname). orderby (C => C. City). tolist ();
It should be noted that there are two orderby orders. When there are multiple orderby operations, the cascade mode is in reverse order. That is, when the city is first ranked, the city is placed at the end.

For descending order, use the corresponding descending operator to replace it immediately. VaR q = dB. MERs. orderbydescending (C => C. City). thenbydescending (C => C. contactname). tolist ();

It must be noted that the orderby operation does not support sorting by type or anonymous classes.
For example, VAR q = dB. Customers. orderby (C => C). tolist (); and
VaR q = dB. MERs. orderby (C => New {C. City, C. contactname}). tolist ();

An exception is thrown. However, since we mentioned this, we will not make mistakes in this case. The common mistake is that the previous operation has an anonymous class. When we compare it with orderby, We will compare the class. For example

VaR q = dB. Customers. Select (C => New {C. City, C. Address}). orderby (C => C). tolist ();

If you want to use orderby (C => C), the prerequisite is that the class of the generated object in the previous step must be the basic type of the C # language. For example
VaR q = dB. Customers. Select (C => C. City). orderby (C => C). tolist ();
City is of the string type.

In addition, there is a slight difference between the orderby and dlinq operations. LINQ supports sorting by type. However, you need to implement the icomparable interface by yourself.

For example, the statement: var q = dB. MERs. tolist (). orderby (C => C). tolist ();
The first tolist () will retrieve all the data in the database and put it in the memory. All subsequent operations will be performed on the memory. All subsequent operations are LINQ operations, not dlinq. (As mentioned in the previous article) If you want to sort by customer, you must implement the icomparable interface in the customer class.

Its customer class must be inherited from icomparable. The Code is as follows,
Public partial class customers: system. Data. LINQ. inotifypropertychanging, system. componentmodel. inotifypropertychanged, icomparable
The icomparable interface is implemented as follows: # region icomparable members

Public int compareto (Object OBJ)
{
Return this. _ customerid. compareto (customers) OBJ). customerid );
// Throw new exception ("the method or operation is not implemented .");
}

# Endregion

The orderby operation automatically calls the method of this interface to sort by category. If this interface is not implemented in your ing file, the system will throw an exception.
You can also use generic, as shown below,
Public partial class MERs: system. Data. LINQ. inotifypropertychanging, system. componentmodel. inotifypropertychanged, icomparable <customers>

# Region icomparable <customers> Members

Public int compareto (customers other)
{
Return this. customerid. compareto (other. customerid );
// Throw new exception ("the method or operation is not implemented .");
}

# Endregion

The advantage is that you do not need to forcibly convert an object into a customer class.
Let's define a new one. Sort the order number first, and sort the same order by the product number first.
Public partial class orderdetails: system. Data. LINQ. inotifypropertychanging, system. componentmodel. inotifypropertychanged, icomparable <orderdetails>

# Region icomparable <orderdetails> Members

Public int compareto (orderdetails other)
{
Int K = This. _ orderid-other. orderid;
If (k = 0)
{
K = This. _ productid-other. productid;
}
Return K;
// Throw new exception ("the method or operation is not implemented .");
}

Now, more functions are available for you to implement. Next time, let's talk about the groupby operation.

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.