QueryableHelper
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的更多相关文章
- MVC实用架构设计(三)——EF-Code First(4):数据查询
前言 首先对大家表示抱歉,这个系列已经将近一个月没有更新了,相信大家等本篇更新都等得快失望了.实在没办法,由于本人水平有限,写篇博客基本上要大半天的时间,最近实在是抽不出这么长段的空闲时间来写.另外也 ...
- c# 扩展方法 奇思妙用 高级篇 九:OrderBy(string propertyName, bool desc)
下面是 Queryable 类 中最常用的两个排序的扩展方法: 1 2 public static IOrderedQueryable<TSource> OrderBy<TSourc ...
- 框架搭建与EF常用基类实现
前两篇简单谈了一些.Net Core的优势以及机构设计的一些思路,这一篇开始,我们将从零开始搭建架构,底层我们将采用EF来访问数据库,所以这篇我们将贴一下EF常用操作的基类. 简单介绍下一些类库将要实 ...
- C#对IQueryable<T>、IEnumerable<T>的扩展方法
#region IQueryable<T>的扩展方法 #region 根据第三方条件是否为真是否执行指定条件的查询 /// <summary> /// 根据第三方条件是否为真是 ...
随机推荐
- [uva] 10099 - The Tourist Guide
10099 - The Tourist Guide 题目页:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemi ...
- TP5:隐藏inde.php文件
原文地址:https://www.cnblogs.com/wangjiayi/p/7943268.html 一,找到/public/.htaccess文件,如果你的入口文件已经移动到根目录下,那么你的 ...
- January 19 2017 Week 3 Thursday
What a man needs most is appreciated. 人性最深切的需求就是渴望别人的赞赏. Being appreciated by others is very importa ...
- macbook pro 2016 2017 15寸 雷电3 外接显卡 epu 简单教程(不修改UEFI)
雷电3外接显卡效果还不错,但是除了akitio node 其他厂家并不会维护自己的固件来适配新机型,我自己买的mbp 2016 15''就出现了和AORUS Gaming Box 1070不兼容的问题 ...
- 一直在用的一个javascript网站
http://www.dottoro.com/ 很不错,例子丰富,解释详细,全面:非常好的参考资料站.
- UML用例图间关系说明
用例间一般存在如下四种关系: 1."通信"关系(<<cmmunicate>>构造型): "通信"关系:使用实心的关联线或带<< ...
- D3——基本知识点
选择器: d3.select - 从当前文档中选择一个元素 d3.selectAll - 从当前文档中选择多个元素 selection.append - 创建并追加一个新元素 selection.at ...
- 用poi-3.6-20091214.jar 实现java给excel资料加密
用poi-3.6-20091214.jar 实现java给excel文件加密我用了网上的很多方法,但是都没有成功! HSSFWorkbook wb = new HSSFWorkbook(new Fil ...
- codeforces 814D An overnight dance in discotheque
题目链接 正解:贪心. 首先我们可以计算出每个圆被多少个圆覆盖. 很显然,最外面的圆是肯定要加上的. 然后第二层的圆也是要加上的.那么第三层就不可能被加上了.同理,第四层的圆又一定会被加上. 然后我们 ...
- Apache 在不同平台和版本上的默认文件布局
https://wiki.apache.org/httpd/DistrosDefaultLayout This guide lists the default installation layouts ...