zoukankan      html  css  js  c++  java
  • 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迁移:

    PM> Enable-Migrations -EnableAutomaticMigrations
    PM> Add-Migration InitialCreate
    PM> Update-Database -Verbose
  • 相关阅读:
    204. Count Primes (Integer)
    203. Remove Linked List Elements (List)
    202. Happy Number (INT)
    201. Bitwise AND of Numbers Range (Bit)
    200. Number of Islands (Graph)
    199. Binary Tree Right Side View (Tree, Stack)
    198. House Robber(Array; DP)
    191. Number of 1 Bits (Int; Bit)
    190. Reverse Bits (Int; Bit)
    189. Rotate Array(Array)
  • 原文地址:https://www.cnblogs.com/nopassword/p/6160008.html
Copyright © 2011-2022 走看看