using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text; namespace Oyang.Tool
{
public class QueryableHelper
{
public static IQueryable<TSource> WhereIf<TSource>(IQueryable<TSource> source, bool isTrue, Expression<Func<TSource, bool>> predicate)
{
if (isTrue)
{
source = source.Provider.CreateQuery<TSource>(
Expression.Call(
null,
Where_TSource_2(typeof(TSource)),
source.Expression, Expression.Quote(predicate)
));
}
return source;
} private static MethodInfo s_Where_TSource_2; private static MethodInfo Where_TSource_2(Type TSource) =>
(s_Where_TSource_2 ??
(s_Where_TSource_2 = new Func<IQueryable<object>, Expression<Func<object, bool>>, IQueryable<object>>(Queryable.Where).GetMethodInfo().GetGenericMethodDefinition()))
.MakeGenericMethod(TSource); public static List<TSource> ToPageList<TSource>(IQueryable<TSource> source, ref int pageIndex, int pageSize, string sortField, bool isAsc, out int totalCount)
{
totalCount = source.Count();
int pageCount = totalCount % pageSize == ? totalCount / pageSize : totalCount / pageSize + ;
if (pageCount > && pageIndex > pageCount)
{
pageIndex = pageCount;
} var param = Expression.Parameter(typeof(TSource));
var body = Expression.Property(param, sortField);
dynamic keySelector = Expression.Lambda(body, param);
source = isAsc ? Queryable.OrderBy(source, keySelector) : Queryable.OrderByDescending(source, keySelector);
source = source.Skip((pageIndex - ) * pageSize).Take(pageSize);
return source.ToList();
} public static List<TSource> ToPageList<TSource>(IQueryable<TSource> source, IPagination p)
{
int pageIndex = p.PageIndex;
List<TSource> temp = ToPageList<TSource>(source, ref pageIndex, p.PageSize, p.SortField, p.IsAsc, out int totalCount);
p.TotalCount = totalCount;
p.PageIndex = pageIndex;
return temp;
}
}
}

QueryableHelper的更多相关文章

  1. MVC实用架构设计(三)——EF-Code First(4):数据查询

    前言 首先对大家表示抱歉,这个系列已经将近一个月没有更新了,相信大家等本篇更新都等得快失望了.实在没办法,由于本人水平有限,写篇博客基本上要大半天的时间,最近实在是抽不出这么长段的空闲时间来写.另外也 ...

  2. c# 扩展方法 奇思妙用 高级篇 九:OrderBy(string propertyName, bool desc)

    下面是 Queryable 类 中最常用的两个排序的扩展方法: 1 2 public static IOrderedQueryable<TSource> OrderBy<TSourc ...

  3. 框架搭建与EF常用基类实现

    前两篇简单谈了一些.Net Core的优势以及机构设计的一些思路,这一篇开始,我们将从零开始搭建架构,底层我们将采用EF来访问数据库,所以这篇我们将贴一下EF常用操作的基类. 简单介绍下一些类库将要实 ...

  4. C#对IQueryable<T>、IEnumerable<T>的扩展方法

    #region IQueryable<T>的扩展方法 #region 根据第三方条件是否为真是否执行指定条件的查询 /// <summary> /// 根据第三方条件是否为真是 ...

随机推荐

  1. freess(未测试)

    freess 使用 nodejs 配合 shadowsocks-windows 实现FQ (windows) 使用方法: 如果你没有安装nodejs请先安装,访问 https://nodejs.org ...

  2. 缓存服务Ehcache方案

    1  Ehcache简介 在Java项目广泛的使用.它是一个开源的.设计于提高在数据从RDBMS中取出来的高花费.高延迟采取的一种缓存方案.正因为Ehcache具有健壮性(基于java 开发).被认证 ...

  3. SQLSERVER 中GO的作用

    go 向 SQL Server 实用工具发出一批 Transact-SQL 语句结束的信号.go是把t-sql语句分批次执行.(一步成功了才会执行下一步,即一步一个go) BEGIN 和 END 语句 ...

  4. 【Leetcode】【Medium】Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 解题 ...

  5. 【Leetcode】【Medium】3Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  6. July 04th 2017 Week 27th Tuesday

    Nothing is really beautiful but truth. 只有真理才是真美. Truth can be beautiful, but it also can be cruel. W ...

  7. 如何写Paper

    如何写文章,如何写好文章,是每一个科研工作者想弄懂或者已经弄懂了的问题.剑桥大学某研究人员分享了他的写作思路. 我从该视频中学到了以下几点经验: 正确的顺序是:Idea——>Write——> ...

  8. 修改WSAD的默认工作区(转)

    如果要一直用某个目录作为工作区,可以在启动WSAD的时候将该目录设为默认工作区,这样启动时直接启动,方便.快捷.但是如果设置后在想换工作区就比较麻烦了,因为没有可供选择的界面了,跟网上搜了一下总结共有 ...

  9. 如何用jstl的select标签做二级联动下拉列表框??

    下拉列表框的多级联动早就会了.但是用jstl的select标签做下拉列表框的做二级联动的时候还是遇到了些问题.主要问题在用Ajax查询到的数据如何拼成下拉选项的时候.其实很简单,但我还是折腾了好久.所 ...

  10. Powershell split file

    #网上所得非原创 function split($inFile, $outPrefix, [Int32] $bufSize){ $stream = [System.IO.File]::OpenRead ...