using Admin.Core.Common; using Admin.Core.Model; using SqlSugar; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; using Admin.Core.IRepository; using NPOI.SS.Formula.Functions; using Org.BouncyCastle.Crypto; using Castle.DynamicProxy.Generators.Emitters.SimpleAST; namespace Admin.Core.Repository { public class BaseRepository : IBaseRepository where TEntity : class, new() { private readonly IUnitOfWork _unitOfWork; private SqlSugarClient _dbBase; private ISqlSugarClient _db { get { /* 如果要开启多库支持, * 1、在appsettings.json 中开启MutiDBEnabled节点为true,必填 * 2、设置一个主连接的数据库ID,节点MainDB,对应的连接字符串的Enabled也必须true,必填 */ if (Appsettings.app(new string[] { "MutiDBEnabled" }).ObjToBool()) { if (typeof(TEntity).GetTypeInfo().GetCustomAttributes(typeof(SugarTable), true).FirstOrDefault((x => x.GetType() == typeof(SugarTable))) is SugarTable sugarTable && !string.IsNullOrEmpty(sugarTable.TableDescription)) { _dbBase.ChangeDatabase(sugarTable.TableDescription.ToLower()); } else { _dbBase.ChangeDatabase(MainDb.CurrentDbConnId.ToLower()); } } return _dbBase; } } public ISqlSugarClient Db { get { return _db; } } public BaseRepository(IUnitOfWork unitOfWork) { _unitOfWork = unitOfWork; _dbBase = unitOfWork.GetDbClient(); } #region 异步方法 public async Task QueryByIdAsync(object objId) { //return await Task.Run(() => _db.CopyNew().Queryable().InSingle(objId)); return await _db.CopyNew().Queryable().In(objId).SingleAsync(); } /// /// 功能描述:根据ID查询一条数据 /// 作  者:Admin.Core /// /// id(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件 /// 是否使用缓存 /// 数据实体 public async Task QueryByIdAsync(object objId, bool blnUseCache = false) { //return await Task.Run(() => _db.CopyNew().Queryable().WithCacheIF(blnUseCache).InSingle(objId)); return await _db.CopyNew().Queryable().WithCacheIF(blnUseCache).In(objId).SingleAsync(); } /// /// 功能描述:根据ID查询数据 /// 作  者:Admin.Core /// /// id列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件 /// 数据实体列表 public async Task> QueryByIDsAsync(object[] lstIds) { //return await Task.Run(() => _db.CopyNew().Queryable().In(lstIds).ToList()); return await _db.CopyNew().Queryable().In(lstIds).ToListAsync(); } /// /// 写入实体数据 /// /// 博文实体类 /// public async Task AddAsync(TEntity entity) { //var i = await Task.Run(() => _db.CopyNew().Insertable(entity).ExecuteReturnBigIdentity()); ////返回的i是long类型,这里你可以根据你的业务需要进行处理 //return (int)i; var insert = _db.CopyNew().Insertable(entity); //这里你可以返回TEntity,这样的话就可以获取id值,无论主键是什么类型 //var return3 = await insert.ExecuteReturnEntityAsync(); return await insert.ExecuteReturnIdentityAsync(); } /// /// 写入实体数据 /// /// 实体类 /// 指定只插入列 /// 返回自增量列 public async Task AddAsync(TEntity entity, Expression> insertColumns = null) { var insert = _db.CopyNew().Insertable(entity); if (insertColumns == null) { return await insert.ExecuteReturnIdentityAsync(); } else { return await insert.InsertColumns(insertColumns).ExecuteReturnIdentityAsync(); } } /// /// 批量插入实体(速度快) /// /// 实体集合 /// 影响行数 public async Task AddAsync(List listEntity) { return await _db.CopyNew().Insertable(listEntity.ToArray()).ExecuteCommandAsync(); } /// /// 更新实体数据 /// /// 博文实体类 /// public async Task UpdateAsync(TEntity entity) { ////这种方式会以主键为条件 //var i = await Task.Run(() => _db.CopyNew().Updateable(entity).ExecuteCommand()); //return i > 0; //这种方式会以主键为条件 return await _db.CopyNew().Updateable(entity).ExecuteCommandHasChangeAsync(); } public async Task UpdateAsync(TEntity entity, string strWhere) { //return await Task.Run(() => _db.CopyNew().Updateable(entity).Where(strWhere).ExecuteCommand() > 0); return await _db.CopyNew().Updateable(entity).Where(strWhere).ExecuteCommandHasChangeAsync(); } public async Task UpdateAsync(string strSql, SugarParameter[] parameters = null) { //return await Task.Run(() => _db.CopyNew().Ado.ExecuteCommand(strSql, parameters) > 0); return await _db.CopyNew().Ado.ExecuteCommandAsync(strSql, parameters) > 0; } public async Task UpdateAsync(object operateAnonymousObjects) { return await _db.CopyNew().Updateable(operateAnonymousObjects).ExecuteCommandAsync() > 0; } public async Task UpdateAsync( TEntity entity, List lstColumns = null, List lstIgnoreColumns = null, string strWhere = "" ) { //IUpdateable up = await Task.Run(() => _db.CopyNew().Updateable(entity)); //if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0) //{ // up = await Task.Run(() => up.IgnoreColumns(it => lstIgnoreColumns.Contains(it))); //} //if (lstColumns != null && lstColumns.Count > 0) //{ // up = await Task.Run(() => up.UpdateColumns(it => lstColumns.Contains(it))); //} //if (!string.IsNullOrEmpty(strWhere)) //{ // up = await Task.Run(() => up.Where(strWhere)); //} //return await Task.Run(() => up.ExecuteCommand()) > 0; IUpdateable up = _db.CopyNew().Updateable(entity); if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0) { up = up.IgnoreColumns(lstIgnoreColumns.ToArray()); } if (lstColumns != null && lstColumns.Count > 0) { up = up.UpdateColumns(lstColumns.ToArray()); } if (!string.IsNullOrEmpty(strWhere)) { up = up.Where(strWhere); } return await up.ExecuteCommandHasChangeAsync(); } /// /// 正序查询第一条数据 /// /// public async Task FirstAsync() { return await _db.CopyNew().Queryable().FirstAsync(); } /// /// 查询第一条数据 /// /// /// public async Task FirstAsync(Expression> whereExpression) { return await _db.CopyNew().Queryable().WhereIF(whereExpression != null, whereExpression).FirstAsync(); } /// /// 根据实体删除一条数据 /// /// 博文实体类 /// public async Task DeleteAsync(TEntity entity) { //var i = await Task.Run(() => _db.CopyNew().Deleteable(entity).ExecuteCommand()); //return i > 0; return await _db.CopyNew().Deleteable(entity).ExecuteCommandHasChangeAsync(); } /// /// 删除指定ID的数据 /// /// 主键ID /// public async Task DeleteByIdAsync(object id) { //var i = await Task.Run(() => _db.CopyNew().Deleteable(id).ExecuteCommand()); //return i > 0; return await _db.CopyNew().Deleteable(id).ExecuteCommandHasChangeAsync(); } /// /// 删除指定ID集合的数据(批量删除) /// /// 主键ID集合 /// public async Task DeletesAsync(List entitys) { //var i = await Task.Run(() => _db.CopyNew().Deleteable().In(ids).ExecuteCommand()); //return i > 0; return await _db.CopyNew().Deleteable(entitys).ExecuteCommandHasChangeAsync(); } /// /// 功能描述:查询所有数据 /// 作  者:Admin.Core /// /// 数据列表 public async Task> QueryAsync() { return await _db.CopyNew().Queryable().ToListAsync(); } /// /// 功能描述:查询数据列表 /// 作  者:Admin.Core /// /// 条件 /// 数据列表 public async Task> QueryAsync(string strWhere) { //return await Task.Run(() => _db.CopyNew().Queryable().WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList()); return await _db.CopyNew().Queryable().WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToListAsync(); } /// /// 功能描述:查询数据列表 /// 作  者:Admin.Core /// /// whereExpression /// 数据列表 public async Task> QueryAsync(Expression> whereExpression) { return await _db.CopyNew().Queryable().WhereIF(whereExpression != null, whereExpression).ToListAsync(); } /// /// 功能描述:按照特定列查询数据列表 /// 作  者:Admin.Core /// /// /// /// public async Task> QueryAsync(Expression> expression) { return await _db.CopyNew().Queryable().Select(expression).ToListAsync(); } /// /// 功能描述:按照特定列查询数据列表带条件排序 /// 作  者:Admin.Core /// /// /// 过滤条件 /// 查询实体条件 /// 排序条件 /// public async Task> QueryAsync(Expression> expression, Expression> whereExpression, string strOrderByFileds) { return await _db.CopyNew().Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Select(expression).ToListAsync(); } /// /// 功能描述:查询一个列表 /// 作  者:Admin.Core /// /// 条件表达式 /// 排序字段,如name asc,age desc /// 数据列表 public async Task> QueryAsync(Expression> whereExpression, string strOrderByFileds) { //return await Task.Run(() => _db.CopyNew().Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToList()); return await _db.CopyNew().Queryable().WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).ToListAsync(); } /// /// 功能描述:查询一个列表 /// /// /// /// /// public async Task> QueryAsync(Expression> whereExpression, Expression> orderByExpression, bool isAsc = true) { //return await Task.Run(() => _db.CopyNew().Queryable().OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).WhereIF(whereExpression != null, whereExpression).ToList()); return await _db.CopyNew().Queryable().OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).WhereIF(whereExpression != null, whereExpression).ToListAsync(); } /// /// 功能描述:查询一个列表 /// 作  者:Admin.Core /// /// 条件 /// 排序字段,如name asc,age desc /// 数据列表 public async Task> QueryAsync(string strWhere, string strOrderByFileds) { //return await Task.Run(() => _db.CopyNew().Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList()); return await _db.CopyNew().Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToListAsync(); } /// /// 功能描述:查询前N条数据 /// 作  者:Admin.Core /// /// 条件表达式 /// 前N条 /// 排序字段,如name asc,age desc /// 数据列表 public async Task> QueryAsync( Expression> whereExpression, int intTop, string strOrderByFileds) { //return await Task.Run(() => _db.CopyNew().Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Take(intTop).ToList()); return await _db.CopyNew().Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Take(intTop).ToListAsync(); } /// /// 功能描述:查询前N条数据 /// 作  者:Admin.Core /// /// 条件 /// 前N条 /// 排序字段,如name asc,age desc /// 数据列表 public async Task> QueryAsync( string strWhere, int intTop, string strOrderByFileds) { //return await Task.Run(() => _db.CopyNew().Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).Take(intTop).ToList()); return await _db.CopyNew().Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).Take(intTop).ToListAsync(); } /// /// 根据sql语句查询 /// /// 完整的sql语句 /// 参数 /// 泛型集合 public async Task> QuerySqlAsync(string strSql, SugarParameter[] parameters = null) { return await _db.CopyNew().Ado.SqlQueryAsync(strSql, parameters); } /// /// 执行sql语句 /// /// 完整的sql语句 /// 参数 /// 泛型集合 public async Task ExecSqlAsync(string strSql, SugarParameter[] parameters = null) { return await _db.CopyNew().Ado.ExecuteCommandAsync(strSql, parameters); } /// /// 根据sql语句查询 /// /// 完整的sql语句 /// 参数 /// DataTable public async Task QueryTableAsync(string strSql, SugarParameter[] parameters = null) { return await _db.CopyNew().Ado.GetDataTableAsync(strSql, parameters); } /// /// 功能描述:分页查询 /// 作  者:Admin.Core /// /// 条件表达式 /// 页码(下标0) /// 页大小 /// 排序字段,如name asc,age desc /// 数据列表 public async Task> QueryAsync( Expression> whereExpression, int intPageIndex, int intPageSize, string strOrderByFileds) { //return await Task.Run(() => _db.CopyNew().Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageList(intPageIndex, intPageSize)); return await _db.CopyNew().Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageListAsync(intPageIndex, intPageSize); } /// /// 功能描述:分页查询 /// 作  者:Admin.Core /// /// 条件 /// 页码(下标0) /// 页大小 /// 排序字段,如name asc,age desc /// 数据列表 public async Task> QueryAsync( string strWhere, int intPageIndex, int intPageSize, string strOrderByFileds) { //return await Task.Run(() => _db.CopyNew().Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToPageList(intPageIndex, intPageSize)); return await _db.CopyNew().Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToPageListAsync(intPageIndex, intPageSize); } /// /// 分页查询[使用版本,其他分页未测试] /// /// 条件表达式 /// 页码(下标0) /// 页大小 /// 排序字段,如name asc,age desc /// public async Task> QueryPageAsync(Expression> whereExpression, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null) { RefAsync totalCount = 0; var list = await _db.CopyNew().Queryable() .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds) .WhereIF(whereExpression != null, whereExpression) .ToPageListAsync(intPageIndex, intPageSize, totalCount); int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt(); return new PageModel() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, pageSize = intPageSize, data = list }; } /// ///查询-多表查询 /// /// 实体1 /// 实体2 /// 实体3 /// 返回对象 /// 关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo} /// 返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo} /// 查询表达式 (w1, w2) =>w1.UserNo == "") /// public async Task> QueryMuchAsync( Expression> joinExpression, Expression> selectExpression, Expression> whereLambda = null) where T : class, new() { if (whereLambda == null) { return await _db.CopyNew().Queryable(joinExpression).Select(selectExpression).ToListAsync(); } return await _db.CopyNew().Queryable(joinExpression).Where(whereLambda).Select(selectExpression).ToListAsync(); } /// /// 两表联合查询-分页 /// /// 实体1 /// 实体1 /// 返回对象 /// 关联表达式 /// 返回表达式 /// 查询表达式 /// 页码 /// 页大小 /// 排序字段 /// public async Task> QueryTabsPageAsync( Expression> joinExpression, Expression> selectExpression, Expression> whereExpression, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null) { RefAsync totalCount = 0; var list = await _db.CopyNew().Queryable(joinExpression) .Select(selectExpression) .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds) .WhereIF(whereExpression != null, whereExpression) .ToPageListAsync(intPageIndex, intPageSize, totalCount); int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt(); return new PageModel() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, pageSize = intPageSize, data = list }; } /// /// 两表联合查询-分页-分组 /// /// 实体1 /// 实体1 /// 返回对象 /// 关联表达式 /// 返回表达式 /// 查询表达式 /// 页码 /// 页大小 /// 排序字段 /// public async Task> QueryTabsPageAsync( Expression> joinExpression, Expression> selectExpression, Expression> whereExpression, Expression> groupExpression, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null) { RefAsync totalCount = 0; var list = await _db.CopyNew().Queryable(joinExpression).GroupBy(groupExpression) .Select(selectExpression) .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds) .WhereIF(whereExpression != null, whereExpression) .ToPageListAsync(intPageIndex, intPageSize, totalCount); int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt(); return new PageModel() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, pageSize = intPageSize, data = list }; } #endregion #region 同步方法 public TEntity QueryById(object objId) { return _db.CopyNew().Queryable().In(objId).Single(); } /// ().WithCacheIF(blnUseCache).In(objId).Single(); } /// QueryByIDs(object[] lstIds) { return _db.CopyNew().Queryable().In(lstIds).ToList(); } /// Add(TEntity entity) { //var insert = _db.CopyNew().Insertable(entity); //return insert.ExecuteReturnIdentity(); try { return await _db.Insertable(entity).ExecuteCommandAsync(); } catch (Exception ex) { return 0; } } /// > insertColumns = null) { var insert = _db.CopyNew().Insertable(entity); if (insertColumns == null) { return insert.ExecuteReturnIdentity(); } else { return insert.InsertColumns(insertColumns).ExecuteReturnIdentity(); } } /// listEntity) { return _db.CopyNew().Insertable(listEntity.ToArray()).ExecuteCommand(); } /// 0; } public bool Update(object operateAnonymousObjects) { return _db.CopyNew().Updateable(operateAnonymousObjects).ExecuteCommand() > 0; } public bool Update( TEntity entity, List lstColumns = null, List lstIgnoreColumns = null, string strWhere = "" ) { IUpdateable up = _db.CopyNew().Updateable(entity); if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0) { up = up.IgnoreColumns(lstIgnoreColumns.ToArray()); } if (lstColumns != null && lstColumns.Count > 0) { up = up.UpdateColumns(lstColumns.ToArray()); } if (!string.IsNullOrEmpty(strWhere)) { up = up.Where(strWhere); } return up.ExecuteCommandHasChange(); } /// (id).ExecuteCommandHasChange(); } /// entitys) { //var i = Task.Run(() = _db.CopyNew().Deleteable(entitys).ExecuteCommandHasChange(); } /// Query() { return _db.CopyNew().Queryable().ToList(); } /// Query(string strWhere) { //return Task.Run(() = _db.CopyNew().Queryable().WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList(); } /// Query(Expression> whereExpression) { return _db.CopyNew().Queryable().WhereIF(whereExpression != null, whereExpression).ToList(); } /// Query(Expression> expression) { return _db.CopyNew().Queryable().Select(expression).ToList(); } /// Query(Expression> expression, Expression> whereExpression, string strOrderByFileds) { return _db.CopyNew().Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Select(expression).ToList(); } /// Query(Expression> whereExpression, string strOrderByFileds) { //return Task.Run(() = _db.CopyNew().Queryable().WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).ToList(); } /// Query(Expression> whereExpression, Expression> orderByExpression, bool isAsc = true) { //return Task.Run(() = _db.CopyNew().Queryable().OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).WhereIF(whereExpression != null, whereExpression).ToList(); } /// Query(string strWhere, string strOrderByFileds) { //return Task.Run(() = _db.CopyNew().Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList(); } /// Query( Expression> whereExpression, int intTop, string strOrderByFileds) { //return Task.Run(() = _db.CopyNew().Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Take(intTop).ToList(); } /// Query( string strWhere, int intTop, string strOrderByFileds) { //return Task.Run(() = _db.CopyNew().Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).Take(intTop).ToList(); } /// QuerySql(string strSql, SugarParameter[] parameters = null) { return _db.CopyNew().Ado.SqlQuery(strSql, parameters); } /// Query( Expression> whereExpression, int intPageIndex, int intPageSize, string strOrderByFileds) { //return Task.Run(() = _db.CopyNew().Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageList(intPageIndex, intPageSize); } /// Query( string strWhere, int intPageIndex, int intPageSize, string strOrderByFileds) { //return Task.Run(() = _db.CopyNew().Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToPageList(intPageIndex, intPageSize); } /// QueryPage(Expression> whereExpression, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null) { int totalCount = 0; var list = _db.CopyNew().Queryable() .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds) .WhereIF(whereExpression != null, whereExpression) .ToPageList(intPageIndex, intPageSize, ref totalCount); int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt(); return new PageModel() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, pageSize = intPageSize, data = list }; } /// QueryMuch( Expression> joinExpression, Expression> selectExpression, Expression> whereLambda = null) where T : class, new() { if (whereLambda == null) { return _db.CopyNew().Queryable(joinExpression).Select(selectExpression).ToList(); } return _db.CopyNew().Queryable(joinExpression).Where(whereLambda).Select(selectExpression).ToList(); } /// QueryTabsPage( Expression> joinExpression, Expression> selectExpression, Expression> whereExpression, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null) { int totalCount = 0; var list = _db.CopyNew().Queryable(joinExpression) .Select(selectExpression) .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds) .WhereIF(whereExpression != null, whereExpression) .ToPageList(intPageIndex, intPageSize, ref totalCount); int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt(); return new PageModel() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, pageSize = intPageSize, data = list }; } /// QueryTabsPage( Expression> joinExpression, Expression> selectExpression, Expression> whereExpression, Expression> groupExpression, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null) { int totalCount = 0; var list = _db.CopyNew().Queryable(joinExpression).GroupBy(groupExpression) .Select(selectExpression) .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds) .WhereIF(whereExpression != null, whereExpression) .ToPageList(intPageIndex, intPageSize,ref totalCount); int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt(); return new PageModel() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, pageSize = intPageSize, data = list }; } #endregion } }