Comparison of LINQ sorting methods with lambda sorting and the use of order by order and ThenBy

Source: Internet
Author: User

Follow the entity classes of the previous article with the EF operation Class code. Add a few data to the database

The sort method of Linq, the following example is based on Roleid ascending, name descending

Efcontext<member> Efmembercontext =NewEfcontext<member>();varMemberSet = Efmembercontext.set<member> (). Include ("Role");varMemberlist = fromMinchMemberSet byM.roleid Ascending, M.name descendingSelectm;foreach(Member Iteminchmemberlist) {Console.WriteLine ("{0},role:{1}", item. Name,item. Role.name);}

The output results are as follows:

The SQL Profiler is as follows:

The same way, if you sort by lambda, guess the following,

 efcontext<member> efmembercontext = new  efcontext<member> ();  var  memberset = efmembercontext.set<member> (). Include ( " role   "  var  memberlist = Memberset.orderby (M = M.roleid). OrderByDescending (M => m.name);  foreach  (Member item in   Memberlist) {Console.WriteLine ( Span> " {0},role:{1}   " ,item. Name,item. Role.name);}   

Running the program finds inconsistencies with the LINQ approach,

SQL profile is as follows:

After discovering successive calls to order or orderbydescending, the last one is sorted, and ThenBy is in handy.

ThenBy the results are sorted two times after using the order or orderbydescending

 efcontext<member> efmembercontext = new  efcontext<member> ();  var  memberset = efmembercontext.set<member> (). Include ( " role   "  var  memberlist = Memberset.orderby (M = M.roleid). ThenByDescending (M => m.name);  foreach  (Member item in   Memberlist) {Console.WriteLine ( Span> " {0},role:{1}   " ,item. Name,item. Role.name);}   

If you want to sort more than one column in a database, you can first order (or orderbydescending) the ThenBy (or thenbydescending). You can also specify more than one property directly in order or orderbydescending, so that the resulting SQL is the sort of multiple columns (sort-consistent asc/desc)

 efcontext<member> efmembercontext = new  efcontext<member> ();  var  memberset = efmembercontext.set<member> (). Include ( " role   "  var  memberlist = Memberset.orderby (m = new   foreach  (Member item in   Memberlist) {Console.WriteLine ( Span> " {0},role:{1}   " ,item. Name,item. Role.name);}   

Comparison of LINQ sorting methods with lambda sorting and the use of order by order and ThenBy

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.