EF 通用帮助类 含分页 Lambda 拼接表达式
Posted 322829
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EF 通用帮助类 含分页 Lambda 拼接表达式相关的知识,希望对你有一定的参考价值。
/// <summary>
/// 测试 Lambda 拼接表达式
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
// POST api/values
[HttpPost]
public ListPageResponse<Base_VersionControl> Post([FromBody]SelectVersionControlRequest request)
ListPageResponse<Base_VersionControl> response = new ListPageResponse<Base_VersionControl>();
int Total = 0;
//排序
Expression<Func<Base_VersionControl, int>> order = ex => ex.ID;
var orderlist = PageQueryService.LoadPageItem(request, request.PageSize, request.CurrentPage, out Total, true, order);
response.Datas = orderlist;
response.Total = Total;
return response;
/// <summary>
/// 获取where ambda
/// </summary>
/// <typeparam name="TRequest"></typeparam>
/// <typeparam name="T"></typeparam>
/// <param name="request"></param>
/// <returns></returns>
public static Expression<Func<T, bool>> GetLambdaByRequest<TRequest, T>(TRequest request) where T : class
Expression<Func<T, bool>> where = ex => true;
foreach (PropertyInfo info in typeof(TRequest).GetProperties())
object value = info.GetValue(request, null);
if (info.Name != "CurrentPage" && info.Name != "PageSize" && value != null && value.ToString() != "")
if (info.Name == "Title")
where = where.And(LambdaUtil<T>.In(info.Name, value.ToString().Split(",")));
else
where = where.And(LambdaUtil<T>.Equal(info.Name, info.GetValue(request, null)));
return where;
/// <summary>
/// 分页查询
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="pageSize">每页多少行</param>
/// <param name="pageIndex">第几页</param>
/// <param name="total"></param>
/// /// <param name="whereLambda">条件 lambda表达式</param>
/// <param name="orderByLambda">排序 lambda表达式</param>
/// /// <param name="isAsc">是否升序</param>
/// <returns></returns>
public static List<T> LoadPageItem<T, TRequest, TKey>(TRequest request, int pageSize, int pageIndex, out int total, bool isAsc, Expression<Func<T, TKey>> orderbyLambda) where T : class
SqlserverModel sqlserver = new SqlserverModel();
Expression<Func<T, bool>> whereLambda = GetLambdaByRequest<TRequest, T>(request);
total = sqlserver.Set<T>().Where(whereLambda).Count();
//倒序或升序
if (isAsc)
var temp = sqlserver.Set<T>().Where(whereLambda)
.OrderBy<T, TKey>(orderbyLambda)
.Skip(pageSize * (pageIndex - 1))
.Take(pageSize);
return temp.AsQueryable().ToList();
else
var temp = sqlserver.Set<T>().Where(whereLambda)
.OrderByDescending<T, TKey>(orderbyLambda)
.Skip(pageSize * (pageIndex - 1))
.Take(pageSize);
return temp.AsQueryable().ToList();
以上是关于EF 通用帮助类 含分页 Lambda 拼接表达式的主要内容,如果未能解决你的问题,请参考以下文章
用Springboot+Jpa实现学生CRUD操作(含前端页面,含分页,自定义SQL)