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> /// 根据第三方条件是否为真是 ...
随机推荐
- 139.00.007 Git学习-Cheat Sheet
@(139 - Environment Settings | 环境配置) Git虽然极其强大,命令繁多,但常用的就那么十来个,掌握好这十几个常用命令,你已经可以得心应手地使用Git了. 友情附赠国外网 ...
- 实现UILabel渐变色效果
实现UILabel渐变色效果 效果如下图: 源码: // // CombinationView.h // ChangeColorLabel // // Created by YouXianMing o ...
- C# 操作Excel 格式
数字(Range.NumberFormatlocal 属性)常规:Range.NumberFormatlocal = "G/通用格式"数值:Range.NumberFormatlo ...
- VS2010 调试启动特别慢
调试选项里有 _NT_SYMBOL_PATH 这一项,并且不能取消选择.只好删除这个环境变量,此来源于windbg环境中需要.重启windows后,VS2010调试里已没有此项,F5调试飞快--
- June 23rd 2017 Week 25th Friday
Life doesn't get easier, you just get stronger. 生活从未变得轻松,是你在一点一点变得坚强. So in the same way we can get ...
- 关于数据库插入sql操作速度的影响
大概看了以下,适当多线程数据库连接操作比单线程效率高 多个sql语句组合后调用数据库连接执行比单个sql循环执行效率高的多 下面是几个参考资料,有空的时候详细整理一下 https://blog.csd ...
- Android HttpClient自己主动登陆discuz论坛!
你登陆论坛的时候,我们先看看浏览器干了什么事儿: 用Firefox打开HiPda 的登陆页面,输入用户名和password,点登陆. 以下是通过firebug插件获取的数据: 能够看到浏览器这个htt ...
- Windows彻底卸载系统自带的office
由于自带office导致按照新的office会提示要先卸载原来32位的office,又在控制面板或软件管理工具中找不到office,用如下方法删除 1.在C盘删除office文件夹 2.删除注册表 1 ...
- Python 输出中文的笔记
import sysreload(sys)sys.setdefaultencoding('utf8') 导入csv乱码: 加入: import codecs csvfile.write(codecs. ...
- stixel上边缘
上图是2^x-1的曲线,取值范围在(-1,正无穷) 上面两个公式组成了隶属函数(membership)表示隶属度,隶属度就是衡量这个点同下边缘点是否属于同一个物体.实际上M函数就是2^x-1,但M函数 ...