using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text; namespace Common
{
public class QueryParameters
{
public QueryParameters(); public string order { get; set; }
public int page { get; set; }
public int rows { get; set; }
public string sort { get; set; }
} public class DataGrid<T>
{
/// <summary>
/// 默认构造函数
/// </summary>
public DataGrid() { } /// <summary>
/// 构造函数
/// </summary>
/// <param name="rows">表格数据</param>
public DataGrid(List<T> rows)
{
this.rows = rows;
}
private List<T> _rows = new List<T>(); /// <summary>
/// 表格数据
/// </summary>
public List<T> rows
{
get { return _rows; }
set
{
if (value != null)
{
_rows = value;
}
}
} /// <summary>
/// 总记录数
/// </summary>
public int total { get; set; } public static DataGrid<T> QueryWithParameters(IEnumerable<T> queryable, QueryParameters queryParameters)
{
if (queryable == null)
{
return new DataGrid<T>();
}
queryParameters = queryParameters ?? new QueryParameters();
IQueryable<T> source = Queryable.AsQueryable<T>(queryable); var page = queryParameters.page;
var rows = queryParameters.rows;
var sort = queryParameters.sort;
var order = queryParameters.order; int num = source.Count(); int count = (page - ) * rows; IQueryable<T> s = source; if (!string.IsNullOrWhiteSpace(sort))
{
var sortExpression = Expression.Parameter(source.ElementType);
var selector = Expression.Lambda(Expression.PropertyOrField(sortExpression, sort), sortExpression);
if (order.ToLower() == "asc")
{
s = (IQueryable<T>)source.Provider.CreateQuery(Expression.Call(typeof(Queryable), "OrderBy", new Type[] { source.ElementType, selector.Body.Type }, source.Expression, selector));
}
else if (order.ToLower() == "desc")
{
s = (IQueryable<T>)source.Provider.CreateQuery(Expression.Call(typeof(Queryable), "OrderByDescending", new Type[] { source.ElementType, selector.Body.Type }, source.Expression, selector));
}
} //方法1
s = count == ? s.Take(rows) : s.Skip(count).Take(rows);
//方法2
s = count == ? Queryable.Take<T>(s, rows) : Queryable.Take<T>(Queryable.Skip<T>(s, count), rows); return new DataGrid<T>()
{
total = num,
rows = s.ToList()
};
}
}
}

调用:

DataGrid<T>.QueryWithParameters(query, paging)

Lambda动态排序分页通用方法的更多相关文章

  1. List对象排序的通用方法

    转自 @author chenchuang import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Me ...

  2. 动态调用WebService 通用方法Moss 中 传统开发中都可用。

    WebService是啥大家都知道了,这里不做过多的解释.通常我们使用WebService的做法基本都是在我们的项目中添加Web引用的方式,首先找到WebService的地址,然后定义命名空间,这样会 ...

  3. Lambda动态排序通用方法

    using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...

  4. EF增删改查+使用Expression进行动态排序分页

    注:以下部分来自<ASP.NET MVC 企业级实战>一书的摘抄和改写以及部分个人学习心得. EF简单增删改查 增加 public static int Add() { using (No ...

  5. 使用Expression进行动态排序分页

    Expression动态查询.分页 Expression,表达式树,以lamda表达式创建,就以表达式目录树的形式将强类型的lambda表达式标识为数据结构. 排序 /// <summary&g ...

  6. Lambda动态排序

    private static IList<T> IListOrderBy<T>(IList<T> list, string propertyName) where ...

  7. php sortable 动态排序

    php sortable 动态排序未分页版.php 预览图: <?php mysql_connect("localhost","root","r ...

  8. Linq分页排序通用方法

    1.通用方法 2.调用 -----------------------------1.------------------------------------------- public class ...

  9. Spring data jpa 实现简单动态查询的通用Specification方法

    本篇前提: SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法 这篇文章中的第二种方法 实现Specification 这块的方法 只适用于一个对象针对某一个固定字 ...

随机推荐

  1. DevKit及rails的安装

    Ruby on Rails的安装,是从被称为RubyGems的包管理系统开始的. Ruby on Rails是由Ruby处理系统的类库的.被称为"gem"的格式来进行配置的.&qu ...

  2. HBase 官方文档

    HBase 官方文档 Copyright © 2010 Apache Software Foundation, 盛大游戏-数据仓库团队-颜开(译) Revision History Revision ...

  3. 【总结整理】JQuery基础学习---事件篇

    jQuery鼠标事件之click与dbclick事件 用交互操作中,最简单直接的操作就是点击操作.jQuery提供了两个方法一个是click方法用于监听用户单击操作,另一个方法是dbclick方法用于 ...

  4. Learning Python 006 list(列表) 和 tuple(元组)

    Python list(列表) 和 tuple(元组) list 列表 Python内置的一种数据结构.list:一种有序的集合,可以随时添加和删除其中的元素. list的用法 定义list > ...

  5. Umbraco Examine 实现Fuzzy search

    在Umbraco examine search项目开发中,有一个需求, 就是intercom 和 intercoms需要返回同样的结果 也就是说 搜索intercom 时, 能返回包含intercom ...

  6. idea中java项目增加module后,手动增加xml文件,合并到webapp/WEB-INFO或WEB-INFO(包)

    当手工增加一个module,增加配置文件(如:web.xml)需要合并到文件夹里,要不众多文件在一个src文件夹下,太凌乱. 1. 合并到webapp/WEB-INFO下 a. 首先增加webapp目 ...

  7. Asp.net Core 启动流程分析

    新建的.net core 程序启动本质上是一个控制台应用程序,所以它的入口在Main方法中,所以启动的开始时从Main方法开始. public class Program { public stati ...

  8. kolla base目录下Dockerfile.j2分析

    这几天在研究kolla制作镜像的流程,记录下对kolla项目中base目录Dockerfile.j2阅读过程.本质上base目录下的Dockerfile.j2文件,对是yum 源的一些配置,从而使制作 ...

  9. Element-ui Cascader 级联选择器 同时返回 value 和 label

    给 cascader 组件赋值一个别名 ref="cascaderAddr" <el-form-item label="部门" prop="or ...

  10. Codeforces Round #558 (Div. 2)C(计算几何,排列组合,模拟)

    #include<bits/stdc++.h>using namespace std;typedef struct{ double k,b;}node;node k[1000007];bo ...