小编Cih*_*gun的帖子

C#Linq OrderBy过滤null或空值为last

我尝试使用自定义orderby扩展方法,我成功地处理了我的代码,但另外我想在结果中列出null或空值或零值,任何人都可以帮我解决这个问题?

这是我对orderby的扩展方法

    public static IQueryable<T> OrderBy<T>(this IQueryable<T> q, string SortField, bool isAsc)
    {
        //var nullExpr = Expression.Constant(null, typeof(T));
        var param = Expression.Parameter(typeof(T), "p");
        var prop = Expression.Property(param, SortField);
        var exp = Expression.Lambda(prop, param);
        string method = isAsc ? "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);
    }
Run Code Online (Sandbox Code Playgroud)

提前致谢

c# linq expression

28
推荐指数
2
解决办法
2万
查看次数

标签 统计

c# ×1

expression ×1

linq ×1