IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]c# 扩展LINQ的order by函数支持传递列名作为字符串

    csharp25发表于 2017-03-30 21:49:40
    love 0
     public static class LinQHelper
        {
            public static IQueryable<T> OrderByEx<T>(this IQueryable<T> q, string direction, string fieldName)
            {
                var param = Expression.Parameter(typeof(T), "p");
                var prop = Expression.Property(param, fieldName);
                var exp = Expression.Lambda(prop, param);
                string method = direction.ToLower() == "asc" ? "OrderBy" : "OrderByDescending";
                Type[] types = new Type[] { q.ElementType, exp.Body.Type };
                var mce = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
                return q.Provider.CreateQuery<T>(mce);
            }
        }



沪ICP备19023445号-2号
友情链接