C # LINQ Query dynamic by

Source: Internet
Author: User

Grouplist is the raw data set,list<t>

SortOrder is the sort type, desc or ASC

Sortname is the Sort property name

1. Use reflection.
Private Static ObjectGetPropertyValue (ObjectObjstringProperty ) {System.Reflection.PropertyInfo PropertyInfo=obj. GetType ().      GetProperty (property); returnPropertyinfo.getvalue (obj,NULL);} varResultlist = SortOrder = ="desc"? Grouplist.orderbydescending (p = getpropertyvalue (P, sortname)): Grouplist.orderby (p =GetPropertyValue (P, sortname));//LINQ mode://varResultList1 = fromPinchGrouplist byGetPropertyValue (p, M.sortname)Selectp;if(SortOrder = ="desc") ResultList1= fromPinchGrouplist byGetPropertyValue (p, Sortname)Descending SelectP
2. Call AsQueryable ()

Converts a generic system.collections.generic.ienumerable<t> to a generic system.linq.iqueryable<t>.

var groupquerylist = grouplist.asqueryable (); //  Here var tmplist = groupquerylist. (Sortname, SortOrder);

Reference: http://stackoverflow.com/questions/41244/dynamic-linq-orderby-on-ienumerablet#41262

C # LINQ Query dynamic 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.