using System.Linq.Expressions; using SqlSugar; using Entity.DbModel; using Entity.Common; namespace Repository { public abstract class BaseRepository where T : class, new() { //private readonly IUnitOfWork _unitOfWork; protected BaseRepository(ISqlSugarClient sqlSugar) { //_unitOfWork = unitOfWork; DbBaseClient = sqlSugar; } private ISqlSugarClient DbBaseClient; /// /// 根据主值查询单条数据 /// /// 主键值 /// 是否使用WITH(NOLOCK) /// 泛型实体 public T QueryById(object pkValue, bool blUseNoLock = false) { return DbBaseClient .Queryable() .WithNoLockOrNot(blUseNoLock) .InSingle(pkValue); } /// /// 根据主值查询单条数据 /// /// id(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件 /// 是否使用WITH(NOLOCK) /// 数据实体 public async Task QueryByIdAsync(object objId, bool blUseNoLock = false) { return await DbBaseClient .Queryable() .In(objId) .WithNoLockOrNot(blUseNoLock) .SingleAsync(); } /// /// 根据主值列表查询单条数据 /// /// id列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件 /// 数据实体列表 /// 是否使用WITH(NOLOCK) public List QueryByIDs(object[] lstIds, bool blUseNoLock = false) { return DbBaseClient .Queryable() .In(lstIds) .WithNoLockOrNot(blUseNoLock) .ToList(); } /// /// 根据主值列表查询单条数据 /// /// id列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件 /// 数据实体列表 /// 是否使用WITH(NOLOCK) public async Task> QueryByIDsAsync(object[] lstIds, bool blUseNoLock = false) { return await DbBaseClient .Queryable() .In(lstIds) .WithNoLockOrNot(blUseNoLock) .ToListAsync(); } /// /// 根据主值列表查询单条数据 /// /// id列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件 /// 数据实体列表 /// 是否使用WITH(NOLOCK) public List QueryByIDs(int[] lstIds, bool blUseNoLock = false) { return DbBaseClient .Queryable() .In(lstIds) .WithNoLockOrNot(blUseNoLock) .ToList(); } /// /// 根据主值列表查询单条数据 /// /// id列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件 /// 数据实体列表 /// 是否使用WITH(NOLOCK) public async Task> QueryByIDsAsync(int[] lstIds, bool blUseNoLock = false) { return await DbBaseClient .Queryable() .In(lstIds) .WithNoLockOrNot(blUseNoLock) .ToListAsync(); } /// /// 查询表单所有数据(无分页,请慎用) /// /// 是否使用WITH(NOLOCK) /// public List Query(bool blUseNoLock = false) { return DbBaseClient .Queryable() .WithNoLockOrNot(blUseNoLock) .ToList(); } /// /// 查询表单所有数据(无分页,请慎用) /// /// 是否使用WITH(NOLOCK) /// public async Task> QueryAsync(bool blUseNoLock = false) { return await DbBaseClient .Queryable() .WithNoLockOrNot(blUseNoLock) .ToListAsync(); } /// /// 根据条件查询数据 /// /// 条件 /// 排序字段,如name asc,age desc /// 是否使用WITH(NOLOCK) /// 泛型实体集合 public List QueryListByClause(string strWhere, string orderBy = "", bool blUseNoLock = false) { return DbBaseClient .Queryable() .OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) .WhereIF(!string.IsNullOrEmpty(strWhere), strWhere) .WithNoLockOrNot(blUseNoLock) .ToList(); } /// /// 根据条件查询数据 /// /// 条件 /// 排序字段,如name asc,age desc /// 泛型实体集合 /// 是否使用WITH(NOLOCK) public async Task> QueryListByClauseAsync(string strWhere, string orderBy = "", bool blUseNoLock = false) { return await DbBaseClient .Queryable() .OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) .WhereIF(!string.IsNullOrEmpty(strWhere), strWhere) .WithNoLockOrNot(blUseNoLock) .ToListAsync(); } /// /// 根据条件查询数据 /// /// 条件表达式树 /// 排序字段,如name asc,age desc /// 泛型实体集合 /// 是否使用WITH(NOLOCK) public List QueryListByClause(Expression> predicate, string orderBy = "", bool blUseNoLock = false) { return DbBaseClient .Queryable() .OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) .WhereIF(predicate != null, predicate) .WithNoLockOrNot(blUseNoLock) .ToList(); } /// /// 根据条件查询数据 /// /// 条件表达式树 /// 排序字段,如name asc,age desc /// 泛型实体集合 /// 是否使用WITH(NOLOCK) public async Task> QueryListByClauseAsync(Expression> predicate, string orderBy = "", bool blUseNoLock = false) { return await DbBaseClient .Queryable() .OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) .WhereIF(predicate != null, predicate) .WithNoLockOrNot(blUseNoLock) .ToListAsync(); } /// /// 根据条件查询数据 /// /// 条件表达式树 /// 排序字段 /// 排序顺序 /// 是否使用WITH(NOLOCK) /// 泛型实体集合 public List QueryListByClause(Expression> predicate, Expression> orderByPredicate, OrderByType orderByType, bool blUseNoLock = false) { return DbBaseClient .Queryable() .OrderByIF(orderByPredicate != null, orderByPredicate, orderByType) .WhereIF(predicate != null, predicate) .WithNoLockOrNot(blUseNoLock) .ToList(); } /// /// 根据条件查询数据 /// /// 条件表达式树 /// 排序字段 /// 排序顺序 /// 是否使用WITH(NOLOCK) /// 泛型实体集合 public async Task> QueryListByClauseAsync(Expression> predicate, Expression> orderByPredicate, OrderByType orderByType, bool blUseNoLock = false) { return await DbBaseClient .Queryable() .OrderByIF(orderByPredicate != null, orderByPredicate, orderByType) .WhereIF(predicate != null, predicate) .WithNoLockOrNot(blUseNoLock) .ToListAsync(); } /// /// 根据条件查询一定数量数据 /// /// 条件表达式树 /// 获取数量 /// 排序字段 /// 排序顺序 /// 是否使用WITH(NOLOCK) /// public List QueryListByClause(Expression> predicate, int take, Expression> orderByPredicate, OrderByType orderByType, bool blUseNoLock = false) { return DbBaseClient .Queryable() .OrderByIF(orderByPredicate != null, orderByPredicate, orderByType) .WhereIF(predicate != null, predicate) .Take(take) .WithNoLockOrNot(blUseNoLock) .ToList(); } /// /// 根据条件查询一定数量数据 /// /// 条件表达式树 /// 获取数量 /// 排序字段 /// 排序顺序 /// 是否使用WITH(NOLOCK) /// public async Task> QueryListByClauseAsync(Expression> predicate, int take, Expression> orderByPredicate, OrderByType orderByType, bool blUseNoLock = false) { return await DbBaseClient .Queryable() .OrderByIF(orderByPredicate != null, orderByPredicate, orderByType) .WhereIF(predicate != null, predicate) .Take(take) .WithNoLockOrNot(blUseNoLock) .ToListAsync(); } /// /// 根据条件查询一定数量数据 /// /// 条件表达式树 /// 获取数量 /// 排序字段,如name asc,age desc /// 是否使用WITH(NOLOCK) /// public List QueryListByClause(Expression> predicate, int take, string strOrderByFileds = "", bool blUseNoLock = false) { return DbBaseClient .Queryable() .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds) .Where(predicate) .Take(take) .WithNoLockOrNot(blUseNoLock) .ToList(); } /// /// 根据条件查询一定数量数据 /// /// 条件表达式树 /// 获取数量 /// 排序字段,如name asc,age desc /// 是否使用WITH(NOLOCK) /// public async Task> QueryListByClauseAsync(Expression> predicate, int take, string strOrderByFileds = "", bool blUseNoLock = false) { return await DbBaseClient .Queryable() .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds) .Where(predicate) .Take(take) .WithNoLockOrNot(blUseNoLock) .ToListAsync(); } /// /// 根据条件查询数据 /// /// 条件表达式树 /// 是否使用WITH(NOLOCK) /// public T QueryByClause(Expression> predicate, bool blUseNoLock = false) { return DbBaseClient .Queryable() .WithNoLockOrNot(blUseNoLock) .First(predicate); } /// /// 根据条件查询数据 /// /// 条件表达式树 /// 是否使用WITH(NOLOCK) /// public async Task QueryByClauseAsync(Expression> predicate, bool blUseNoLock = false) { return await DbBaseClient .Queryable() .WithNoLockOrNot(blUseNoLock) .FirstAsync(predicate); } /// /// 根据条件查询数据 /// /// 条件表达式树 /// 排序字段 /// 排序顺序 /// 是否使用WITH(NOLOCK) /// public T QueryByClause(Expression> predicate, Expression> orderByPredicate, OrderByType orderByType, bool blUseNoLock = false) { return DbBaseClient .Queryable() .OrderBy(orderByPredicate, orderByType) .WithNoLockOrNot(blUseNoLock) .First(predicate); } /// /// 根据条件查询数据 /// /// 条件表达式树 /// 排序字段 /// 排序顺序 /// 是否使用WITH(NOLOCK) /// public async Task QueryByClauseAsync(Expression> predicate, Expression> orderByPredicate, OrderByType orderByType, bool blUseNoLock = false) { return await DbBaseClient .Queryable() .OrderBy(orderByPredicate, orderByType) .WithNoLockOrNot(blUseNoLock) .FirstAsync(predicate); } /// /// 写入实体数据 /// /// 实体数据 /// public int Insert(T entity) { return DbBaseClient .Insertable(entity) .ExecuteReturnIdentity(); } /// /// 写入实体数据 /// /// 实体数据 /// public async Task InsertAsync(T entity) { return await DbBaseClient .Insertable(entity) .ExecuteReturnIdentityAsync(); } /// /// 写入实体数据 /// /// 实体数据 /// 插入的列 /// public int Insert(T entity, Expression> insertColumns = null) { var insert = DbBaseClient.Insertable(entity); if (insertColumns == null) return insert.ExecuteReturnIdentity(); return insert.InsertColumns(insertColumns).ExecuteReturnIdentity(); } /// /// 写入实体数据 /// /// 实体数据 /// 插入的列 /// public async Task InsertAsync(T entity, Expression> insertColumns = null) { var insert = DbBaseClient.Insertable(entity); if (insertColumns == null) return await insert.ExecuteReturnIdentityAsync(); return await insert.InsertColumns(insertColumns).ExecuteReturnIdentityAsync(); } /// /// 写入实体数据 /// /// 实体类 /// 需插入的字段 /// public bool InsertGuid(T entity, Expression> insertColumns = null) { var insert = DbBaseClient.Insertable(entity); if (insertColumns == null) return insert.ExecuteCommand() > 0; return insert.InsertColumns(insertColumns).ExecuteCommand() > 0; } /// /// 写入实体数据 /// /// 实体类 /// 需插入的字段 /// public async Task InsertGuidAsync(T entity, Expression> insertColumns = null) { var insert = DbBaseClient.Insertable(entity); if (insertColumns == null) return await insert.ExecuteCommandAsync() > 0; return await insert.InsertColumns(insertColumns).ExecuteCommandAsync() > 0; } /// /// 批量写入实体数据 /// /// 实体类 /// public int Insert(List entity) { return DbBaseClient.Insertable(entity.ToArray()).ExecuteReturnIdentity(); } /// /// 批量写入实体数据 /// /// 实体类 /// public async Task InsertAsync(List entity) { return await DbBaseClient.Insertable(entity.ToArray()).ExecuteCommandAsync(); } /// /// 批量写入实体数据 /// /// 实体类 /// public async Task InsertCommandAsync(List entity) { return await DbBaseClient.Insertable(entity.ToArray()).ExecuteCommandAsync(); } /// /// 批量更新实体数据 /// /// /// public bool Update(List entity) { return DbBaseClient.Updateable(entity).ExecuteCommandHasChange(); } /// /// 批量更新实体数据 /// /// /// public async Task UpdateAsync(List entity) { return await DbBaseClient.Updateable(entity).ExecuteCommandHasChangeAsync(); } /// /// 更新实体数据 /// /// /// public bool Update(T entity) { return DbBaseClient.Updateable(entity).ExecuteCommandHasChange(); } /// /// 更新实体数据 /// /// /// public async Task UpdateAsync(T entity) { return await DbBaseClient.Updateable(entity).ExecuteCommandHasChangeAsync(); } /// /// 根据手写条件更新 /// /// /// /// public bool Update(T entity, string strWhere) { return DbBaseClient.Updateable(entity).Where(strWhere).ExecuteCommandHasChange(); } /// /// 根据手写条件更新 /// /// /// /// public async Task UpdateAsync(T entity, string strWhere) { return await DbBaseClient.Updateable(entity).Where(strWhere).ExecuteCommandHasChangeAsync(); } /// /// 根据手写sql语句更新数据 /// /// /// /// public bool Update(string strSql, SugarParameter[] parameters = null) { return DbBaseClient.Ado.ExecuteCommand(strSql, parameters) > 0; } /// /// 根据手写sql语句更新数据 /// /// /// /// public async Task UpdateAsync(string strSql, SugarParameter[] parameters = null) { return await DbBaseClient.Ado.ExecuteCommandAsync(strSql, parameters) > 0; } /// /// 更新某个字段 /// /// lamdba表达式,如it => new Student() { Name = "a", CreateTime = DateTime.Now } /// lamdba判断 /// public bool Update(Expression> columns, Expression> where) { var i = DbBaseClient.Updateable().SetColumns(columns).Where(where).ExecuteCommand(); return i > 0; } /// /// 更新某个字段 /// /// lamdba表达式,如it => new Student() { Name = "a", CreateTime = DateTime.Now } /// lamdba判断 /// public async Task UpdateAsync(Expression> columns, Expression> where) { return await DbBaseClient.Updateable().SetColumns(columns).Where(where).ExecuteCommandHasChangeAsync(); } /// /// 根据条件更新 /// /// /// /// /// /// public async Task UpdateAsync(T entity, List lstColumns = null, List lstIgnoreColumns = null, string strWhere = "") { var up = DbBaseClient.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 bool Update(T entity, List lstColumns = null, List lstIgnoreColumns = null, string strWhere = "") { var up = DbBaseClient.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(); } /// /// 删除数据 /// /// 实体类 /// public bool Delete(T entity) { return DbBaseClient.Deleteable(entity).ExecuteCommandHasChange(); } /// /// 删除数据 /// /// 实体类 /// public async Task DeleteAsync(T entity) { return await DbBaseClient.Deleteable(entity).ExecuteCommandHasChangeAsync(); } /// /// 删除数据 /// /// 实体类 /// public bool Delete(IEnumerable entity) { return DbBaseClient.Deleteable(entity).ExecuteCommandHasChange(); } /// /// 删除数据 /// /// 实体类 /// public async Task DeleteAsync(IEnumerable entity) { return await DbBaseClient.Deleteable(entity).ExecuteCommandHasChangeAsync(); } /// /// 删除数据 /// /// 过滤条件 /// public bool Delete(Expression> where) { return DbBaseClient.Deleteable(where).ExecuteCommandHasChange(); } /// /// 删除数据 /// /// 过滤条件 /// public async Task DeleteAsync(Expression> where) { return await DbBaseClient.Deleteable(where).ExecuteCommandHasChangeAsync(); } /// /// 删除指定ID的数据 /// /// /// public bool DeleteById(object id) { return DbBaseClient.Deleteable(id).ExecuteCommandHasChange(); } /// /// 删除指定ID的数据 /// /// /// public async Task DeleteByIdAsync(object id) { return await DbBaseClient.Deleteable(id).ExecuteCommandHasChangeAsync(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// public bool DeleteByIds(int[] ids) { return DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChange(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// public async Task DeleteByIdsAsync(int[] ids) { return await DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChangeAsync(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// public bool DeleteByIds(long[] ids) { return DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChange(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// public async Task DeleteByIdsAsync(long[] ids) { return await DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChangeAsync(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// public bool DeleteByIds(Guid[] ids) { return DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChange(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// public async Task DeleteByIdsAsync(Guid[] ids) { return await DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChangeAsync(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// public bool DeleteByIds(string[] ids) { return DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChange(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// public async Task DeleteByIdsAsync(string[] ids) { return await DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChangeAsync(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// public bool DeleteByIds(List ids) { return DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChange(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// public async Task DeleteByIdsAsync(List ids) { return await DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChangeAsync(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// public bool DeleteByIds(List ids) { return DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChange(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// public async Task DeleteByIdsAsync(List ids) { return await DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChangeAsync(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// public bool DeleteByIds(List ids) { return DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChange(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// public async Task DeleteByIdsAsync(List ids) { return await DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChangeAsync(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// public bool DeleteByIds(List ids) { return DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChange(); } /// /// 删除指定ID集合的数据(批量删除) /// /// /// public async Task DeleteByIdsAsync(List ids) { return await DbBaseClient.Deleteable().In(ids).ExecuteCommandHasChangeAsync(); } /// /// 判断数据是否存在 /// /// 条件表达式树 /// 是否使用WITH(NOLOCK) /// public bool Exists(Expression> predicate, bool blUseNoLock = false) { return DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).Any(); } /// /// 判断数据是否存在 /// /// 条件表达式树 /// 是否使用WITH(NOLOCK) /// public async Task ExistsAsync(Expression> predicate, bool blUseNoLock = false) { return await DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).AnyAsync(); } /// /// 获取数据总数 /// /// 条件表达式树 /// 是否使用WITH(NOLOCK) /// public int GetCount(Expression> predicate, bool blUseNoLock = false) { return DbBaseClient.Queryable().WithNoLockOrNot(blUseNoLock).Count(predicate); } /// /// 获取数据总数 /// /// 条件表达式树 /// 是否使用WITH(NOLOCK) /// public async Task GetCountAsync(Expression> predicate, bool blUseNoLock = false) { return await DbBaseClient.Queryable().WithNoLockOrNot(blUseNoLock).CountAsync(predicate); } /// /// 获取数据某个字段的合计 /// /// 条件表达式树 /// 字段 /// 是否使用WITH(NOLOCK) /// public int GetSum(Expression> predicate, Expression> field, bool blUseNoLock = false) { return DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).Sum(field); } /// /// 获取数据某个字段的合计 /// /// 条件表达式树 /// 字段 /// 是否使用WITH(NOLOCK) /// public async Task GetSumAsync(Expression> predicate, Expression> field, bool blUseNoLock = false) { return await DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).SumAsync(field); } /// /// 获取数据某个字段的合计 /// /// 条件表达式树 /// 字段 /// 是否使用WITH(NOLOCK) /// public decimal GetSum(Expression> predicate, Expression> field, bool blUseNoLock = false) { return DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).Sum(field); } /// /// 获取数据某个字段的合计 /// /// 条件表达式树 /// 字段 /// 是否使用WITH(NOLOCK) /// public async Task GetSumAsync(Expression> predicate, Expression> field, bool blUseNoLock = false) { return await DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).SumAsync(field); } /// /// 获取数据某个字段的合计 /// /// 条件表达式树 /// 字段 /// 是否使用WITH(NOLOCK) /// public float GetSum(Expression> predicate, Expression> field, bool blUseNoLock = false) { return DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).Sum(field); } /// /// 获取数据某个字段的合计 /// /// 条件表达式树 /// 字段 /// 是否使用WITH(NOLOCK) /// public async Task GetSumAsync(Expression> predicate, Expression> field, bool blUseNoLock = false) { return await DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).SumAsync(field); } /// /// 根据条件查询分页数据 /// /// /// /// 当前页面索引 /// 分布大小 /// 是否使用WITH(NOLOCK) /// public IPageList QueryPage(Expression> predicate, string orderBy = "", int pageIndex = 1, int pageSize = 20, bool blUseNoLock = false) { var totalCount = 0; var page = DbBaseClient .Queryable() .OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) .WhereIF(predicate != null, predicate) .WithNoLockOrNot(blUseNoLock) .ToPageList(pageIndex, pageSize, ref totalCount); var list = new PageList(page, pageIndex, pageSize, totalCount); return list; } /// /// 根据条件查询分页数据 /// /// /// /// 当前页面索引 /// 分布大小 /// 是否使用WITH(NOLOCK) /// public async Task> QueryPageAsync(Expression> predicate, string orderBy = "", int pageIndex = 1, int pageSize = 20, bool blUseNoLock = false) { RefAsync totalCount = 0; var page = await DbBaseClient .Queryable() .OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) .WhereIF(predicate != null, predicate) .WithNoLockOrNot(blUseNoLock) .ToPageListAsync(pageIndex, pageSize, totalCount); var list = new PageList(page, pageIndex, pageSize, totalCount); return list; } /// /// 根据条件查询分页数据 /// /// 判断集合 /// 排序方式 /// 当前页面索引 /// 分布大小 /// /// 是否使用WITH(NOLOCK) /// public IPageList QueryPage(Expression> predicate, Expression> orderByExpression, OrderByType orderByType, int pageIndex = 1, int pageSize = 20, bool blUseNoLock = false) { var totalCount = 0; var page = DbBaseClient .Queryable() .OrderByIF(orderByExpression != null, orderByExpression, orderByType) .WhereIF(predicate != null, predicate) .WithNoLockOrNot(blUseNoLock) .ToPageList(pageIndex, pageSize, ref totalCount); var list = new PageList(page, pageIndex, pageSize, totalCount); return list; } /// /// 根据条件查询分页数据 /// /// 判断集合 /// 排序方式 /// 当前页面索引 /// 分布大小 /// /// 是否使用WITH(NOLOCK) /// public async Task> QueryPageAsync(Expression> predicate, Expression> orderByExpression, OrderByType orderByType, int pageIndex = 1, int pageSize = 20, bool blUseNoLock = false) { RefAsync totalCount = 0; var page = await DbBaseClient .Queryable() .WhereIF(predicate != null, predicate) .OrderByIF(orderByExpression != null, orderByExpression, orderByType) .WithNoLockOrNot(blUseNoLock) .ToPageListAsync(pageIndex, pageSize, totalCount); var list = new PageList(page, pageIndex, pageSize, totalCount); return list; } /// /// 查询-2表查询 /// /// 实体1 /// 实体2 /// 返回对象 /// 关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo} /// 返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo} /// 查询表达式 (w1, w2) =>w1.UserNo == "") /// 是否使用WITH(NOLOCK) /// public List QueryMuch( Expression> joinExpression, Expression> selectExpression, Expression> whereLambda = null, bool blUseNoLock = false) where T1 : class, new() { return DbBaseClient .Queryable(joinExpression) .WhereIF(whereLambda is not null, whereLambda) .Select(selectExpression) .WithNoLockOrNot(blUseNoLock) .ToList(); } /// /// 查询-多表查询 /// /// 实体1 /// 实体2 /// 返回对象 /// 关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo} /// 返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo} /// 查询表达式 (w1, w2) =>w1.UserNo == "") /// 是否使用WITH(NOLOCK) /// public async Task> QueryMuchAsync( Expression> joinExpression, Expression> selectExpression, Expression> whereLambda = null, bool blUseNoLock = false) where T1 : class, new() { return await DbBaseClient .Queryable(joinExpression) .WhereIF(whereLambda is not null, whereLambda) .Select(selectExpression) .WithNoLockOrNot(blUseNoLock) .ToListAsync(); } /// /// 查询-二表查询 /// /// 实体1 /// 实体2 /// 返回对象 /// 关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo} /// 返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo} /// 查询表达式 (w1, w2) =>w1.UserNo == "") /// 是否使用WITH(NOLOCK) /// public TResult QueryMuchFirst( Expression> joinExpression, Expression> selectExpression, Expression> whereLambda = null, bool blUseNoLock = false) where T1 : class, new() { return DbBaseClient .Queryable(joinExpression) .WhereIF(whereLambda is not null, whereLambda) .Select(selectExpression) .WithNoLockOrNot(blUseNoLock) .First(); } /// /// 查询-二表查询 /// /// 实体1 /// 实体2 /// 返回对象 /// 关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo} /// 返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo} /// 查询表达式 (w1, w2) =>w1.UserNo == "") /// 是否使用WITH(NOLOCK) /// public async Task QueryMuchFirstAsync( Expression> joinExpression, Expression> selectExpression, Expression> whereLambda = null, bool blUseNoLock = false) where T1 : class, new() { return await DbBaseClient .Queryable(joinExpression) .WhereIF(whereLambda is not null, whereLambda) .Select(selectExpression).WithNoLockOrNot(blUseNoLock) .FirstAsync(); } /// /// 查询-三表查询 /// /// 实体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 == "") /// 是否使用WITH(NOLOCK) /// public List QueryMuch( Expression> joinExpression, Expression> selectExpression, Expression> whereLambda = null, bool blUseNoLock = false) where T1 : class, new() { return DbBaseClient .Queryable(joinExpression) .WhereIF(whereLambda is not null, whereLambda) .Select(selectExpression) .WithNoLockOrNot(blUseNoLock) .ToList(); } /// /// 查询-4表查询 /// /// 实体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 == "") /// 是否使用WITH(NOLOCK) /// public List QueryMuch( Expression> joinExpression, Expression> selectExpression, Expression> whereLambda = null, bool blUseNoLock = false) where T1 : class, new() { return DbBaseClient .Queryable(joinExpression) .WhereIF(whereLambda is not null, whereLambda) .Select(selectExpression) .WithNoLockOrNot(blUseNoLock) .ToList(); } /// /// 查询-三表查询 /// /// 实体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 == "") /// 是否使用WITH(NOLOCK) /// public async Task> QueryMuchAsync( Expression> joinExpression, Expression> selectExpression, Expression> whereLambda = null, bool blUseNoLock = false) where T1 : class, new() { return await DbBaseClient .Queryable(joinExpression) .WhereIF(whereLambda is not null, whereLambda) .Select(selectExpression) .WithNoLockOrNot(blUseNoLock) .ToListAsync(); } /// /// 执行sql语句并返回List /// /// /// /// /// public List SqlQuery(string sql, List parameters) { var list = DbBaseClient.Ado.SqlQuery(sql, parameters); return list; } /// /// 执行sql语句并返回List /// /// /// /// public async Task> SqlQueryable(string sql) { var list = await DbBaseClient.SqlQueryable(sql).ToListAsync(); return list; } } }