EF下泛型分页方法,更新方法
Posted 乐哈哈1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EF下泛型分页方法,更新方法相关的知识,希望对你有一定的参考价值。
/// <summary> /// 获取分页的分页集合 /// </summary> /// <typeparam name="S">实体类型</typeparam> /// <param name="filter">过滤,条件</param> /// <param name="pageIndex">当前页</param> /// <param name="pageSize">每页记录数</param> /// <param name="orderByExpression">排序</param> /// <param name="ascending">是否排序</param> /// <returns>返回分页的分页集合</returns> public PagedResult<T> GetFilteredPageResult<S, T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, Expression<Func<T, S>> orderByExpression, bool ascending) where T : class { int count = this.DataContext.Set<T>().Where(filter).Count(); return new PagedResult<T>( pageIndex, pageSize, count, this.GetFilteredElements<S,T>(filter, pageIndex, pageSize, orderByExpression, ascending)); }
/// <summary> /// 获取有条件的分页对象集合 /// </summary> /// <typeparam name="S">实体类型</typeparam> /// <param name="filter">条件过滤</param> /// <param name="pageIndex">当前页</param> /// <param name="pageSize">每页记录数</param> /// <param name="orderByExpression">排序,排序Func表达式</param> /// <param name="ascending">是否排序</param> /// <returns>返回有条件的分页对象集合</returns> public IEnumerable<T> GetFilteredElements<S, T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, Expression<Func<T, S>> orderByExpression, bool ascending) where T : class { if (filter == (Expression<Func<T, bool>>)null) { throw new ArgumentNullException("filter", Messages.exception_FilterCannotBeNull); } if (pageIndex < 0) { throw new ArgumentException(Messages.exception_InvalidPageIndex); } if (pageSize <= 0) { throw new ArgumentException(Messages.exception_InvalidPageSize); } if (orderByExpression == (Expression<Func<T, S>>)null) { throw new ArgumentNullException("orderByExpression", Messages.exception_OrderByExpressionCannotBeNull); } return ascending ? this.DataContext.Set<T>() .Where(filter) .OrderBy(orderByExpression) .Skip((pageIndex - 1) * pageSize) .Take(pageSize) .ToList() : this.DataContext.Set<T>() .Where(filter) .OrderByDescending(orderByExpression) .Skip((pageIndex - 1) * pageSize) .Take(pageSize) .ToList(); } /// <summary> /// 获取记录数 /// </summary> /// <param name="filter">条件filter</param> /// <returns>返回记录数</returns> public int GetRecordCount<T>(Expression<Func<T, bool>> filter) where T : class { return this.DataContext.Set<T>().Where(filter).Count(); }
public class PagedResult<TEntity> { /// <summary> /// 数据列表 /// </summary> public IEnumerable<TEntity> List { get; private set; } /// <summary> /// 每页记录数 /// </summary> public int PageSize { get; private set; } /// <summary> /// 页索引 /// </summary> public int PageIndex { get; private set; } /// <summary> /// 总记录数 /// </summary> public int RecordCount { get; private set; } /// <summary> /// 总页数 /// </summary> public int TotalPages { get { int pages = 0; if (RecordCount > 0 && PageSize > 0) { int temp = RecordCount % PageSize; pages = RecordCount / PageSize; if (temp > 0) { return pages + 1; } } return pages; } } /// <summary> /// Constructor /// </summary> /// <param name="pageIndex">页索引</param> /// <param name="pageSize">每页记录数</param> /// <param name="recordCount">总记录数</param> /// <param name="list">数据列表</param> public PagedResult(int pageIndex, int pageSize, int recordCount, IEnumerable<TEntity> list) { this.List = list; this.PageIndex = pageIndex; this.PageSize = pageSize; this.RecordCount = recordCount; } }
/// <summary>
/// 通过条件批量更新实体的部分值【异步更新】
/// </summary>
/// <param name="filterExpression">更新条件</param>
/// <param name="updateExpression">更新值</param>
public virtual void UpdateAsync<T>(Expression<Func<T, bool>> filterExpression, Expression<Func<T, T>> updateExpression) where T : class
{
DataContext.Set<T>().Where(filterExpression).UpdateAsync(updateExpression);
}
以上是关于EF下泛型分页方法,更新方法的主要内容,如果未能解决你的问题,请参考以下文章