/// <summary> /// 查找分页列表 /// </summary> /// <param>每页记录数。必须大于1</param> /// <param>页码。首页从1开始,页码必须大于1</param> /// <param>总记录数</param> /// <returns></returns> public IQueryable<T> FindPageList(int pageSize, int pageIndex, out int totalNumber) { if (pageIndex < 1) pageIndex = 1; if (pageSize < 1) pageSize = 10; IQueryable<T> _list = DbContext.Set<T>(); totalNumber = _list.Count(); return _list.Skip((pageIndex - 1) * pageIndex).Take(pageSize); } /// <summary> /// 查找分页列表 /// </summary> /// <param>每页记录数。必须大于1</param> /// <param>页码。首页从1开始,页码必须大于1</param> /// <param>总记录数</param> /// <param>排序键</param> /// <param>是否正序</param> /// <returns></returns> public IQueryable<T> FindPageList<TKey>(int pageSize, int pageIndex, out int totalNumber, Expression<Func<T, TKey>> order, bool asc) { if (pageIndex < 1) pageIndex = 1; if (pageSize < 1) pageSize = 10; IQueryable<T> _list = DbContext.Set<T>(); _list = asc ? _list.OrderBy(order) : _list.OrderByDescending(order); totalNumber = _list.Count(); return _list.Skip((pageIndex - 1) * pageIndex).Take(pageSize); } /// <summary> /// 查找分页列表 /// </summary> /// <param>每页记录数。必须大于1</param> /// <param>页码。首页从1开始,页码必须大于1</param> /// <param>总记录数</param> /// <param>查询表达式</param> public IQueryable<T> FindPageList(int pageSize, int pageIndex, out int totalNumber, Expression<Func<T, bool>> where) { if (pageIndex < 1) pageIndex = 1; if (pageSize < 1) pageSize = 10; IQueryable<T> _list = DbContext.Set<T>().Where(where); totalNumber = _list.Count(); return _list.Skip((pageIndex - 1) * pageIndex).Take(pageSize); } /// <summary> /// 查找分页列表 /// </summary> /// <param>每页记录数。必须大于1</param> /// <param>页码。首页从1开始,页码必须大于1</param> /// <param>总记录数</param> /// <param>查询表达式</param> /// <param>排序键</param> /// <param>是否正序</param> public IQueryable<T> FindPageList<TKey>(int pageSize, int pageIndex, out int totalNumber, Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, bool asc) { if (pageIndex < 1) pageIndex = 1; if (pageSize < 1) pageSize = 10; IQueryable<T> _list = DbContext.Set<T>().Where(where); _list = asc ? _list.OrderBy(order) : _list.OrderByDescending(order); totalNumber = _list.Count(); return _list.Skip((pageIndex - 1) * pageIndex).Take(pageSize); }
6、添加实体方法Add
Add 方法有一个重载,重载方法可以选择是否立即保存
/// <summary> /// 添加实体 /// </summary> /// <param>实体</param> /// <returns>受影响的对象的数目</returns> public int Add(T entity) { return Add(entity, true); } /// <summary> /// 添加实体 /// </summary> /// <param>实体</param> /// <param>是否立即保存</param> /// <returns>在“isSave”为True时返回受影响的对象的数目,为False时直接返回0</returns> public int Add(T entity,bool isSave) { DbContext.Set<T>().Add(entity); return isSave ? DbContext.SaveChanges() : 0; }
7、更新实体方法Update
Updae 方法有一个重载,重载方法可以选择是否立即保存
#region Update /// <summary> /// 更新实体【立即保存】 /// </summary> /// <param>实体</param> /// <returns>在“isSave”为True时返回受影响的对象的数目,为False时直接返回0</returns> public int Update(T entity) { return Update(entity, true); } /// <summary> /// 更新实体 /// </summary> /// <param>实体</param> /// <param>是否立即保存</param> /// <returns>在“isSave”为True时返回受影响的对象的数目,为False时直接返回0</returns> public int Update(T entity, bool isSave) { DbContext.Set<T>().Attach(entity); DbContext.Entry<T>(entity).State = EntityState.Modified; return isSave ? DbContext.SaveChanges() : 0; }
8、删除实体方法Delete
Delete 方法有两个重载,一个是可以选择否立即保存,另一个是批量删除