• 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
  • 相关阅读:
    POJ 1775 (ZOJ 2358) Sum of Factorials
    POJ 1844 Sum
    HDOJ 1081(ZOJ 1074) To The Max(动态规划)
    HDOJ 2012 素数判定
    HDOJ 2011 多项式求和
    HDOJ 2010 水仙花数
    马云最新发言:让员工、客户、合作伙伴比自己更强
    乐视手机1S正式发售,乐视商城官网抽风遭网友吐槽
    C++引用(Reference)
    实验三 二叉树的基本操作(建立)及遍历
  • 原文地址:https://www.cnblogs.com/nopassword/p/6160008.html
Copyright © 2020-2023  润新知