//<summary>
//Enable dynamic ordering
//A great God of the source Blog Park, the implementation of the principle is to use the entity and Sort fields to automatically generate an expression
///And then use Iquerable method implementation
// Some are more like Microsoft source ZHANGQC 2016.10.20
//</summary>
public static class Dynamicorder
{
#region Private expression Tree
//<summary>
//build expression tree results similar to j=>j.name
//</SUMMARY>ZHANGQC 2016.10.20
//<typep Aram Name= "TEntity" ></TYPEPARAM>
//<param name= "PropertyName" ></PARAM>
//<param Name= "Resulttype" ></PARAM>
//<returns></returns>
private static LambdaExpression Generateselector<tentity> (String PropertyName, out Type resulttype) where Tentity:class
{
PropertyInfo PR Operty;
Expression propertyaccess;
var parameter = Expression.parameter (typeof (TEntity), "J");
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);
}
//<summary>
//Generate method call
//</summary>
//<typeparam name= "TEntity" ></typeparam& Gt
//<param name= "source" ></PARAM>
//<param name= "MethodName" ></PARAM>
//< param name= "FieldName" ></PARAM>
//<returns></returns>
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, "by", 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. (SortField): result. ThenBy (SortField);
}
}
return result;
}
How to use
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
Sidx
Select New
// {
D.DSOURCE_ID,
D.dsource_name,
Dsource_item = P.prop_description,
D.dsource_description,
Dsource_status = D.dsource_status = = "1"? "Enabled": "Disabled",
D.dsource_expiredday,
Dsource_alarmhigh = (D.dsource_alarmhigh = = -65535 | | d.dsource_alarmhigh = 65535)? "Not disabled": D.dsource_alarmhigh. ToString (),
Dsource_alarmlow = (D.dsource_alarmlow = = -65535 | | d.dsource_alarmhigh = 65535)? "Not disabled": D.dsource_alarmhigh. ToString (),
Dsource_alarmdeltadata = (D.dsource_alarmdeltadata = = -65535 | | d.dsource_alarmhigh = 65535)? "Not disabled": D.dsource_alarmhigh. ToString (),
Dsource_alarmidle = (D.dsource_alarmidle = = -65535 | | d.dsource_alarmhigh = 65535)? "Not 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);
}
Implementing dynamic Sequencing