using System.Linq.Expressions; using HybirdFrameworkCore.Entity; using SqlSugar; namespace Repository; public abstract class BaseRepository where T : class, new() { //private readonly IUnitOfWork _unitOfWork; protected BaseRepository(ISqlSugarClient sqlSugar) { //_unitOfWork = unitOfWork; DbBaseClient = sqlSugar; } public ISqlSugarClient DbBaseClient; /// /// 根据主值查询单条数据 /// /// 主键值 /// 是否使用WITH(NOLOCK) /// 泛型实体 public T QueryById(object pkValue, bool blUseNoLock = false) { return DbBaseClient .Queryable() .WithNoLockOrNot(blUseNoLock) .InSingle(pkValue); } public ISugarQueryable Queryable(bool blUseNoLock = false) { return DbBaseClient .Queryable().WithNoLockOrNot(blUseNoLock); } /// /// 根据主值查询单条数据 /// /// 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(); } /// /// 根据条件查询表单数据(分页) /// /// 页数 /// 每页几条数据 /// 是否使用WITH(NOLOCK) /// public IPage QueryIPageByCause(QueryPageModel page, Expression> predicate) { if (null == predicate) { return this.QueryIPage(page); } int totalCount = 0; List pageList = DbBaseClient .Queryable() .Where(predicate) .WithNoLockOrNot(false) .ToPageList(page.PageNum, page.PageSize, ref totalCount); return new IPage(totalCount, page, pageList); } /// /// 根据条件查询表单数据(分页) 异步 /// /// /// /// public async Task> QueryIPageByCauseAsync(QueryPageModel page, Expression> predicate) { if (null == predicate) { return await this.QueryIPageAsync(page); } RefAsync totalCount = 0; List pageList = await DbBaseClient .Queryable() .Where(predicate) .WithNoLockOrNot(false) .ToPageListAsync(page.PageNum, page.PageSize, totalCount); return new IPage(totalCount, page, pageList); } /// /// 查询表单所有数据(分页) /// /// 页数 /// 每页几条数据 /// 是否使用WITH(NOLOCK) /// public IPage QueryIPage(QueryPageModel page) { int totalCount = 0; //page.Page = page.Page == 0 ? 1 : page.Page;//默认第一页 10条数据 //page.PageSize = page.PageSize == 0 ? 10 : page.PageSize; List pageList = DbBaseClient .Queryable() .WithNoLockOrNot(false) .ToPageList(page.PageNum, page.PageSize, ref totalCount); return new IPage(totalCount, page, pageList); } /// /// 查询表单所有数据(分页) 异步 /// /// /// public async Task> QueryIPageAsync(QueryPageModel page) { RefAsync totalCount = 0; List pageList = await DbBaseClient .Queryable() .WithNoLockOrNot(false) .ToPageListAsync(page.PageNum, page.PageSize, totalCount); return new IPage(totalCount, page, pageList); } /// /// 根据主值列表查询单条数据 /// /// 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(); } public async Task> QueryListByClauseAsync( bool isWhere, Expression> expression, Expression> orderBy, bool blUseNoLock = false) { return await DbBaseClient .Queryable() .OrderBy(orderBy, OrderByType.Asc) .WhereIF(isWhere, expression) .WithNoLockOrNot(blUseNoLock) .Select() .ToListAsync(); } public async Task> QueryListByClauseAsync( Expression> expression, Expression> expression1 ) { return await DbBaseClient .Queryable() .Where(expression) .ToListAsync(expression1); } public async Task> QueryListBySelectClauseAsync(Expression> selectExpression) { return await DbBaseClient .Queryable() .Select(selectExpression) .ToListAsync(); } public async Task> QueryListByOrderClauseAsync(Expression> expression) { return await DbBaseClient .Queryable() .OrderBy(expression, OrderByType.Asc) .ToListAsync(); } public async Task> QueryListByClauseAsync( Expression>> include1, Expression> expression) { return await DbBaseClient .Queryable() .Includes(include1) .OrderBy(expression, OrderByType.Asc) .ToListAsync(); } public async Task> QueryListByInludeClauseAsync( Expression> include1, Expression> expression, Expression> expression2 ) { return await DbBaseClient .Queryable() .Includes(include1) .Where(expression) .OrderBy(expression2, OrderByType.Asc) .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(); } public async Task> QueryListByClauseAsync( bool isWhere, Expression> expression, bool isWhere1, Expression> expression1, bool isWhere2, Expression> expression2, Expression> expression3, bool blUseNoLock = false) { return await DbBaseClient .Queryable() .OrderBy(expression3, OrderByType.Asc) .WhereIF(isWhere, expression) .WhereIF(isWhere1, expression1) .WhereIF(isWhere2, expression2) .WithNoLockOrNot(blUseNoLock) .ToListAsync(); } public async Task> QueryListByClauseAsync( Expression> expression, bool isWhere1, Expression> expression1, bool isWhere2, Expression> expression2, Expression> expression3, int pageNumber, int pageSize, RefAsync totalNumber, bool blUseNoLock = false) { return await DbBaseClient .Queryable() .OrderBy(expression3, OrderByType.Asc) .Where(expression) .WhereIF(isWhere1, expression1) .WhereIF(isWhere2, expression2) .WithNoLockOrNot(blUseNoLock) .ToPageListAsync(pageNumber, pageSize, totalNumber); } public async Task> QueryTreeByClauseAsync( Expression> expression, Expression>> childListExpression, Expression> parentIdExpression, object rootValue) { return await DbBaseClient .Queryable() .OrderBy(expression, OrderByType.Asc) .ToTreeAsync(childListExpression, parentIdExpression, rootValue); } public async Task> QueryTreeByClauseAsync( Expression> expression, Expression>> childListExpression, Expression> parentIdExpression, object rootValue, object[] childIds) { return await DbBaseClient .Queryable() .OrderBy(expression, OrderByType.Asc) .ToTreeAsync(childListExpression, parentIdExpression, rootValue, childIds); } public async Task> QueryTreeByClauseAsync( Expression> parentIdExpression, object primaryKeyValue, bool isContainOneself = true) { return await DbBaseClient .Queryable() .ToChildListAsync(parentIdExpression, primaryKeyValue, isContainOneself); } public async Task> QueryTreeByClauseAsync( Expression> expression, Expression> expression1, Expression>> childListExpression, Expression> parentIdExpression, object rootValue ) { return await DbBaseClient .Queryable() .Where(expression) .OrderBy(expression1, OrderByType.Asc) .ToTreeAsync(childListExpression, parentIdExpression, rootValue); } public async Task> QueryTreeByClauseAsync( Expression> expression, Expression> expression1, Expression>> childListExpression, Expression> parentIdExpression, object rootValue, object[] childIds ) { return await DbBaseClient .Queryable() .Where(expression) .OrderBy(expression1, OrderByType.Asc) .ToTreeAsync(childListExpression, parentIdExpression, rootValue, childIds); } /// /// 根据条件查询数据 /// /// 条件表达式树 /// 排序字段 /// 排序顺序 /// 是否使用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(); } public async Task> QueryListByClauseAsync( Expression> expression, bool isWhere, Expression> whereIfExpression, Expression> selectExpression ) { return await DbBaseClient .Queryable() .Where(expression) .WhereIF(isWhere, whereIfExpression) .Select(selectExpression) .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); } public async Task> QueryByGroupByAsync( Expression> expression, Expression> expression2 ) { return await DbBaseClient .Queryable() .GroupBy(expression) .Select(expression2) .ToListAsync(); } public async Task> QueryByClauseAsync(Expression> predicate, Expression> selectExpression, bool blUseNoLock = false) { return await DbBaseClient .Queryable() .Where(predicate) .Select(selectExpression) .WithNoLockOrNot(blUseNoLock) .ToListAsync(); } public async Task UpdateColumnsAsync( T updateObj, Expression> columns ) { return await DbBaseClient .Updateable(updateObj) .UpdateColumns(columns) .ExecuteCommandAsync(); } public async Task> QueryListByClauseAsync( Expression> expression) { return await DbBaseClient .Queryable() .Where(expression) .ToListAsync(); } /// /// 根据条件查询数据 /// /// 条件表达式树 /// 排序字段 /// 排序顺序 /// 是否使用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 async Task> QueryByOrderByClauseAsync ( Expression> expression, Expression> expression1, bool blUseNoLock = false) { return await DbBaseClient .Queryable() .Where(expression) .OrderBy(expression1, OrderByType.Asc) .WithNoLockOrNot(blUseNoLock) .ToListAsync(); } public List QueryByClauseToList( Expression> expression, Expression> expression2, Expression> expression1, bool blUseNoLock = false) { return DbBaseClient .Queryable() .Where(expression) .Where(expression2) .OrderBy(expression1, OrderByType.Asc) .WithNoLockOrNot(blUseNoLock) .ToList(); } public List QueryByClauseToList(Expression> expression, Expression> expression2, bool blUseNoLock = false) { return DbBaseClient .Queryable() .Where(expression) .Where(expression2) .WithNoLockOrNot(blUseNoLock) .ToList(); } public List QueryByClauseToList(Expression> expression, Expression> expression1, bool blUseNoLock = false) { return DbBaseClient .Queryable() .Where(expression) .OrderBy(expression1, OrderByType.Asc) .WithNoLockOrNot(blUseNoLock) .ToList(); } public List QueryByClauseToList(Expression> expression) { return DbBaseClient .Queryable() .Where(expression) .ToList(); } /// /// /// /// /// /// /// /// /// /// /// public async Task> QueryByClauseAsync( Expression> joinExpression, Expression> whereExpression, Expression> orderExpression, Expression> selectExpression ) { return await DbBaseClient .Queryable() .LeftJoin(joinExpression) .Where(whereExpression) .OrderBy(orderExpression, OrderByType.Asc) .Select(selectExpression) .ToListAsync(); } public async Task> QueryByClauseAsync( Expression> joinExpression, Expression> whereExpression, bool isWhere, Expression> whereifExpression, Expression> orderExpression, Expression> selectExpression ) { return await DbBaseClient .Queryable() .LeftJoin(joinExpression) .Where(whereExpression) .WhereIF(isWhere, whereifExpression) .OrderBy(orderExpression, OrderByType.Asc) .Select(selectExpression) .ToListAsync(); } /// /// 写入实体数据 /// /// 实体数据 /// public T Insert(T entity) { return DbBaseClient .Insertable(entity) .ExecuteReturnEntity(); } /// /// 写入或者更新实体数据 /// /// 实体数据 /// public int InsertOrUpdate(T entity) { return DbBaseClient.Storageable(entity).ExecuteCommand(); } /// /// 写入实体数据 /// /// 实体数据 /// public async Task InsertAsync(T entity) { return await DbBaseClient .Insertable(entity) .ExecuteReturnEntityAsync(); } public async Task InsertReturnEntityAsync(T entity) { return await DbBaseClient .Insertable(entity) .ExecuteReturnEntityAsync(); } /// /// 写入实体数据 /// /// 实体数据 /// 插入的列 /// public T Insert(T entity, Expression> insertColumns = null) { var insert = DbBaseClient.Insertable(entity); if (insertColumns == null) return insert.ExecuteReturnEntity(); return insert.InsertColumns(insertColumns).ExecuteReturnEntity(); } /// /// 写入实体数据 /// /// 实体数据 /// 插入的列 /// public async Task InsertAsync(T entity, Expression> insertColumns = null) { var insert = DbBaseClient.Insertable(entity); if (insertColumns == null) return await insert.ExecuteReturnEntityAsync(); return await insert.InsertColumns(insertColumns).ExecuteReturnEntityAsync(); } /// /// 写入实体数据 /// /// 实体类 /// 需插入的字段 /// 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 bool Insert(List entity) { return DbBaseClient.Insertable(entity.ToArray()).ExecuteCommandIdentityIntoEntity(); ; } /// /// 批量写入实体数据 /// /// 实体类 /// public async Task InsertAsync(List entity) { return await DbBaseClient.Insertable(entity.ToArray()).ExecuteCommandIdentityIntoEntityAsync(); } /// /// 批量写入实体数据 /// /// 实体类 /// 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 async Task UpdateAsync( Expression> expression ) { return await DbBaseClient .Queryable() .ClearFilter() .AnyAsync(expression); } public int Update(Expression> columns, Expression> expression) { return DbBaseClient.Updateable().SetColumns(columns).Where(expression).ExecuteCommand(); } public async Task UpdateAsync( Expression> columns, Expression> expression ) { return await DbBaseClient .Updateable() .SetColumns(columns) .Where(expression) .ExecuteCommandAsync(); } public async Task UpdateAsync( Expression> columns ) { return await DbBaseClient .Updateable() .IgnoreColumns(columns) .ExecuteCommandAsync(); } public async Task UpdateAsync( T updateObj, bool ignoreAllNullColumns ) { return await DbBaseClient .Updateable(updateObj) .IgnoreColumns(ignoreAllNullColumns) .ExecuteCommandAsync(); } public async Task UpdateAsync( T updateObj, bool ignoreAllNullColumns, Expression> columns ) { return await DbBaseClient .Updateable(updateObj) .IgnoreColumns(ignoreAllNullColumns) .IgnoreColumns(columns) .ExecuteCommandAsync(); } /// /// 更新实体数据 /// /// /// 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(); } /// /// 新增方法 /// 该方法用于:根据角色Id删除用户角色 /// /// /// /// /// public async Task DeleteUserRoleByRoleId( Expression> predicate, Expression> selectExpression, Action action) { await DbBaseClient.Queryable() .Where(predicate) .Select(selectExpression) .ForEachAsync(action); } /// /// 删除数据 /// /// 过滤条件 /// 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); } public async Task> QueryPageAsync( Expression> whereExpression, bool isWhere1, Expression> expression1, bool isWhere2, Expression> expression2, bool isWhere3, Expression> expression3, Expression> orderBy, int pageNumber, int pageSize, RefAsync totalNumber, bool blUseNoLock = false) { var page = await DbBaseClient .Queryable() .Where(whereExpression) .WhereIF(isWhere1, expression1) .WhereIF(isWhere2, expression2) .WhereIF(isWhere3, expression3) .OrderBy(orderBy, OrderByType.Asc) .WithNoLockOrNot(blUseNoLock) .ToPageListAsync(pageNumber, pageSize, totalNumber); return page; } /// /// 查询-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; } }