Lambda动态排序通用方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks; namespace DataSort
{
public class DataHelper
{
public static IQueryable<T> Sort<T>(IQueryable<T> queryable, string sort, string order)
{
if (string.IsNullOrWhiteSpace(sort))
{
return queryable;
}
var sortExpression = Expression.Parameter(queryable.ElementType);
var selector = Expression.Lambda(Expression.PropertyOrField(sortExpression, sort), sortExpression);
if (order.ToLower() == "asc")
{
return (IQueryable<T>)queryable.Provider.CreateQuery(Expression.Call(typeof(Queryable), "OrderBy", new Type[] { queryable.ElementType, selector.Body.Type }, queryable.Expression, selector));
}
else if (order.ToLower() == "desc")
{ return (IQueryable<T>)queryable.Provider.CreateQuery(Expression.Call(typeof(Queryable), "OrderByDescending", new Type[] { queryable.ElementType, selector.Body.Type }, queryable.Expression, selector));
}
return queryable;
}
}
}
Lambda动态排序通用方法的更多相关文章
- Linq分页排序通用方法
1.通用方法 2.调用 -----------------------------1.------------------------------------------- public class ...
- List对象排序通用方法
import java.util.Collections; import java.util.Comparator; import java.util.List; import java.lang.r ...
- Lambda动态排序分页通用方法
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...
- Lambda动态排序
private static IList<T> IListOrderBy<T>(IList<T> list, string propertyName) where ...
- linq扩展之动态排序
前两天看QQ群里面,一位朋友问的问题,说在linq中怎么实现动态排序呢,自己想了半天,没有头绪,网上找了下相关的资料,看了下,收益挺多,记录下来. 之前我们没有如果不知道动态排序的方法的话,我们可能会 ...
- List对象排序的通用方法
转自 @author chenchuang import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Me ...
- 写一个针对IQueryable<T>的扩展方法支持动态排序
所谓的动态排序是指支持任意字段.任意升序降序的排序.我们希望在客户端按如下格式写: localhost:8000/api/items?sort=titlelocalhost:8000/api/item ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(17)-LinQ动态排序
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(17)-LinQ动态排序 首先修复程序中的一个BUG这个BUG在GridPager类中,把sord修改为s ...
- LinQ动态排序
LinQ动态排序 首先修复程序中的一个BUG这个BUG在GridPager类中,把sord修改为sort这个名称填写错误,会导致后台一直无法获取datagrid的排序字段 本来是没有这一讲的,为了使2 ...
随机推荐
- python 基础 进程与线程
多进程 使用multipprocessing模块创建多进程 multiprocessing模块提供了一个Process类来描述一个进程对象.创建子进程时,需要传入一个执行函数和函数的参数.用start ...
- C语言学习笔记--void关键字
1.C语言中Void关键字的含义 void 修饰函数返回值和参数——为了表示“无”,如果函数没有返回值,那么应该将其声明为 void,同样的,如果函数没有参数,也应该声明其参数为 void //f() ...
- [poj1390]Blocks(方块消除)
题目大意:给定一序列,可点击某一位置消除与其相邻且相同的方块,得分为$len*len$,求最大得分. 解题关键:关键是状态的构造,首先离散化一下,令$dp[i][j][k]$表示序列$i-j$且后面有 ...
- 第五篇 elasticsearch express插入数据
1.后端 在elasticsearch.js文件夹下添加: function addDocument(document) { return elasticClient.index({ index: i ...
- Hadoop 1.2.1 MapReduce 例子
自学hadoop真的很困难,主要是hadoop版本太混乱了,各个版本之间兼容性并不算太好.更主要的是网上的很多MapReduce的Java例子不写import!!!只写类名!!!偏偏Hadoop中有很 ...
- winDump
windump -i 00-00-10-00-43-A2 监听网卡(一个适配器一个网卡,一个mac)
- 使用VS Code配合Remote Development插件连接远程服务器(Mac/Linux+Windows) | Using VS Code with Remote Development Connect to Remote Server (Mac/Linux+Windows)
最新版VS Code(2019年6月)出了一系列新的插件,包括Remote Development,Remote SSH等,使得用户可以使用VS Code远程连接服务器写代码,方便了协同工作.具体配置 ...
- 常见的web漏洞及其防范
原文地址:http://blog.csdn.net/u013777676/article/details/52124298 一.SQL注入漏洞 SQL注入攻击(SQL Injection),简称注入攻 ...
- 图像标注工具labelImg使用方法
最近在做打标签的工作,为了与大家参考学习,总结了在windows的环境下,基于anaconda的图像标注工具labellmg的一种使用方法! 1 搭建anaconda 以前写过怎么搭建anaconda ...
- PAT L2-006【二叉树中序后序构造树】
#include<bits/stdc++.h> using namespace std; typedef long long LL; struct BT{ int w; BT *L; BT ...