LINQ dynamic orderby sorting

Source: Internet
Author: User

Method:

///   <Summary>
/// Extended sorting of linqstring Fields
///   </Summary>
Public Static Class Stringfieldsorting
{
# Region Private Expression Tree helpers
Private Static Lambdaexpression generateselector <tentity> (string propertyname, Out Type resulttype) Where Tentity: Class
{
Propertyinfo property;
Expression propertyaccess;
VaR Parameter = expression. parameter ( Typeof (Tentity ), " Entity " );

If (Propertyname. Contains ( ' . ' ))
{
String [] childproperties = propertyname. Split ( ' . ' );
Property = Typeof (Tentity). getproperty (childproperties [ 0 ]);
Propertyaccess = expression. makememberaccess (parameter, property );
For ( Int I = 1 ; I <childproperties. length; I ++)
{
Property = property. propertytype. getproperty (childproperties [I]);
Propertyaccess = expression. makememberaccess (propertyaccess, property );
}
}
Else
{
Property = Typeof (Tentity). getproperty (propertyname );
Propertyaccess = expression. makememberaccess (parameter, property );
}

Resulttype = property. propertytype;

Return Expression. Lambda (propertyaccess, parameter );
}
Private Static Methodcallexpression generatemethodcall <tentity> (iqueryable <tentity> source, String Methodname, string fieldname) Where Tentity: Class
{
Type type = Typeof (Tentity );
Type selectorresulttype;
Lambdaexpression selector = generateselector <tentity> (fieldname, Out Selectorresulttype );
Methodcallexpression resultexp = expression. Call ( Typeof (Queryable), methodname,
New Type [] {type, selectorresulttype },
Source. expression, expression. Quote (selector ));
Return Resultexp;
}
# Endregion

Public Static Iorderedqueryable <tentity> orderby <tentity> ( This Iqueryable <tentity> source, String Fieldname) Where Tentity: Class
{
Methodcallexpression resultexp = generatemethodcall <tentity> (source, " Orderby " , Fieldname );
Return Source. provider. createquery <tentity> (resultexp) As Iorderedqueryable <tentity>;
}

Public Static Iorderedqueryable <tentity> orderbydescending <tentity> ( This Iqueryable <tentity> source, String Fieldname) Where Tentity: Class
{
Methodcallexpression resultexp = generatemethodcall <tentity> (source, " Orderbydescending " , Fieldname );
Return Source. provider. createquery <tentity> (resultexp) As Iorderedqueryable <tentity>;
}

Public Static Iorderedqueryable <tentity> thenby <tentity> ( This Iorderedqueryable <tentity> source, String Fieldname) Where Tentity: Class
{
Methodcallexpression resultexp = generatemethodcall <tentity> (source, " Thenby " , Fieldname );
Return Source. provider. createquery <tentity> (resultexp) As Iorderedqueryable <tentity>;
}

Public Static Iorderedqueryable <tentity> thenbydescending <tentity> ( This Iorderedqueryable <tentity> source, String Fieldname) Where Tentity: Class
{
Methodcallexpression resultexp = generatemethodcall <tentity> (source, " Thenbydescending " , Fieldname );
Return Source. provider. createquery <tentity> (resultexp) As Iorderedqueryable <tentity>;
}
Public Static Iorderedqueryable <tentity> orderusingsortexpression <tentity> ( This Iqueryable <tentity> source, String Sortexpression) Where Tentity: Class
{
String [] orderfields = sortexpression. Split ( ' , ' );
Iorderedqueryable <tentity> result = Null ;
For ( Int Currentfieldindex = 0 ; Currentfieldindex <orderfields. length; currentfieldindex ++)
{
String [] expressionpart = orderfields [currentfieldindex]. Trim (). Split ( '   ' );
String sortfield = expressionpart [ 0 ];
Boolean sortdescending = (expressionpart. Length = 2 ) & (Expressionpart [ 1 ]. Equals ( " Desc " , Stringcomparison. ordinalignorecase ));
If (Sortdescending)
{
Result = currentfieldindex = 0 ? Source. orderbydescending (sortfield): result. thenbydescending (sortfield );
}
Else
{
Result = currentfieldindex = 0 ? Source. orderby (sortfield): result. thenby (sortfield );
}
}
Return Result;
}
}

Call:

VaR Query = ( From D In (Vinnotech. gaia2.db. g2_dsource []) result)
Join P In Webapiapplication. Entities. g2_propnames
On D. dsource_item equals P. prop_name
Orderby Sidx
Select New
{
D. dsource_id,
D. dsource_name,
Dsource_item = P. prop_description,
D. dsource_description,
Dsource_status = D. dsource_status = " 1 " ? " Enable " : " Disable " ,
D. dsource_expiredday,
Dsource_alarmhigh = (D. dsource_alarmhigh =- 65535 | D. dsource_alarmhigh = 65535 )? " Disabled " : D. dsource_alarmhigh.tostring (),
Dsource_alarmlow = (D. dsource_alarmlow =- 65535 | D. dsource_alarmhigh = 65535 )? " Disabled " : D. dsource_alarmhigh.tostring (),
Dsource_alarmdeltadata = (D. dsource_alarmdeltadata =- 65535 | D. dsource_alarmhigh = 65535 )? " Disabled " : D. dsource_alarmhigh.tostring (),
Dsource_alarmidle = (D. dsource_alarmidle =- 65535 | D. dsource_alarmhigh = 65535 )? " Disabled " : D. dsource_alarmhigh.tostring (),
D. dsource_formula,
Dsource_updatetime = D. dsource_updatetime.tostring ( " Yyyy-mm-dd hh: mm: SS " )
}). Asqueryable ();

Page = page <= query. Count ()/rows + 1 ? Page: 1 ;

Query = query. orderusingsortexpression ("Dsource_name ASC, dsource_item DESC"). Skip (rows * (page-1). Take (rows );

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.