Linq动态OrderBy排序

方法:

///   <summary>
///  扩展Linqstring字段排序
///   </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;
    }
}

调用:

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 " ?  " 启用 " :  " 禁用 ",
                d.dsource_expiredday,
                dsource_alarmhigh = (d.dsource_alarmhigh == - 65535 || d.dsource_alarmhigh ==  65535) ?  " 未禁用 " : d.dsource_alarmhigh.ToString(),
                dsource_alarmlow = (d.dsource_alarmlow == - 65535 || d.dsource_alarmhigh ==  65535) ?  " 未禁用 " : d.dsource_alarmhigh.ToString(),
                dsource_alarmdeltadata = (d.dsource_alarmdeltadata == - 65535 || d.dsource_alarmhigh ==  65535) ?  " 未禁用 " : d.dsource_alarmhigh.ToString(),
                dsource_alarmidle = (d.dsource_alarmidle == - 65535 || d.dsource_alarmhigh ==  65535) ?  " 未禁用 " : 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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值