From d9f1f8665217e143748185f50610b926c0788352 Mon Sep 17 00:00:00 2001 From: capad Date: Fri, 7 Apr 2023 13:27:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=B8=80=E4=BA=9B=E5=86=97?= =?UTF-8?q?=E4=BD=99=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CoreCms.Net.Repository/BaseRepository.cs | 393 +++++++++---------- CoreCms.Net.Repository/SqlSugarExtensions.cs | 17 + 2 files changed, 213 insertions(+), 197 deletions(-) create mode 100644 CoreCms.Net.Repository/SqlSugarExtensions.cs diff --git a/CoreCms.Net.Repository/BaseRepository.cs b/CoreCms.Net.Repository/BaseRepository.cs index 6d877ab..8b5b118 100644 --- a/CoreCms.Net.Repository/BaseRepository.cs +++ b/CoreCms.Net.Repository/BaseRepository.cs @@ -42,9 +42,10 @@ namespace CoreCms.Net.Repository /// 泛型实体 public T QueryById(object pkValue, bool blUseNoLock = false) { - return blUseNoLock - ? DbBaseClient.Queryable().With(SqlWith.NoLock).InSingle(pkValue) - : DbBaseClient.Queryable().InSingle(pkValue); + return DbBaseClient + .Queryable() + .WithNoLockOrNot(blUseNoLock) + .InSingle(pkValue); } /// @@ -55,9 +56,11 @@ namespace CoreCms.Net.Repository /// 数据实体 public async Task QueryByIdAsync(object objId, bool blUseNoLock = false) { - return blUseNoLock - ? await DbBaseClient.Queryable().In(objId).With(SqlWith.NoLock).SingleAsync() - : await DbBaseClient.Queryable().In(objId).SingleAsync(); + return await DbBaseClient + .Queryable() + .In(objId) + .WithNoLockOrNot(blUseNoLock) + .SingleAsync(); } /// @@ -68,9 +71,11 @@ namespace CoreCms.Net.Repository /// 是否使用WITH(NOLOCK) public List QueryByIDs(object[] lstIds, bool blUseNoLock = false) { - return blUseNoLock - ? DbBaseClient.Queryable().In(lstIds).With(SqlWith.NoLock).ToList() - : DbBaseClient.Queryable().In(lstIds).ToList(); + return DbBaseClient + .Queryable() + .In(lstIds) + .WithNoLockOrNot(blUseNoLock) + .ToList(); } /// @@ -81,9 +86,11 @@ namespace CoreCms.Net.Repository /// 是否使用WITH(NOLOCK) public async Task> QueryByIDsAsync(object[] lstIds, bool blUseNoLock = false) { - return blUseNoLock - ? await DbBaseClient.Queryable().In(lstIds).With(SqlWith.NoLock).ToListAsync() - : await DbBaseClient.Queryable().In(lstIds).ToListAsync(); + return await DbBaseClient + .Queryable() + .In(lstIds) + .WithNoLockOrNot(blUseNoLock) + .ToListAsync(); } /// @@ -94,9 +101,11 @@ namespace CoreCms.Net.Repository /// 是否使用WITH(NOLOCK) public List QueryByIDs(int[] lstIds, bool blUseNoLock = false) { - return blUseNoLock - ? DbBaseClient.Queryable().In(lstIds).With(SqlWith.NoLock).ToList() - : DbBaseClient.Queryable().In(lstIds).ToList(); + return DbBaseClient + .Queryable() + .In(lstIds) + .WithNoLockOrNot(blUseNoLock) + .ToList(); } /// @@ -107,9 +116,11 @@ namespace CoreCms.Net.Repository /// 是否使用WITH(NOLOCK) public async Task> QueryByIDsAsync(int[] lstIds, bool blUseNoLock = false) { - return blUseNoLock - ? await DbBaseClient.Queryable().In(lstIds).With(SqlWith.NoLock).ToListAsync() - : await DbBaseClient.Queryable().In(lstIds).ToListAsync(); + return await DbBaseClient + .Queryable() + .In(lstIds) + .WithNoLockOrNot(blUseNoLock) + .ToListAsync(); } /// @@ -119,9 +130,10 @@ namespace CoreCms.Net.Repository /// public List Query(bool blUseNoLock = false) { - return blUseNoLock - ? DbBaseClient.Queryable().With(SqlWith.NoLock).ToList() - : DbBaseClient.Queryable().ToList(); + return DbBaseClient + .Queryable() + .WithNoLockOrNot(blUseNoLock) + .ToList(); } /// @@ -131,9 +143,10 @@ namespace CoreCms.Net.Repository /// public async Task> QueryAsync(bool blUseNoLock = false) { - return blUseNoLock - ? await DbBaseClient.Queryable().With(SqlWith.NoLock).ToListAsync() - : await DbBaseClient.Queryable().ToListAsync(); + return await DbBaseClient + .Queryable() + .WithNoLockOrNot(blUseNoLock) + .ToListAsync(); } /// @@ -145,11 +158,12 @@ namespace CoreCms.Net.Repository /// 泛型实体集合 public List QueryListByClause(string strWhere, string orderBy = "", bool blUseNoLock = false) { - return blUseNoLock - ? DbBaseClient.Queryable().OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) - .WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).With(SqlWith.NoLock).ToList() - : DbBaseClient.Queryable().OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) - .WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList(); + return DbBaseClient + .Queryable() + .OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) + .WhereIF(!string.IsNullOrEmpty(strWhere), strWhere) + .WithNoLockOrNot(blUseNoLock) + .ToList(); } /// @@ -162,11 +176,12 @@ namespace CoreCms.Net.Repository public async Task> QueryListByClauseAsync(string strWhere, string orderBy = "", bool blUseNoLock = false) { - return blUseNoLock - ? await DbBaseClient.Queryable().OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) - .WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).With(SqlWith.NoLock).ToListAsync() - : await DbBaseClient.Queryable().OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) - .WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToListAsync(); + return await DbBaseClient + .Queryable() + .OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) + .WhereIF(!string.IsNullOrEmpty(strWhere), strWhere) + .WithNoLockOrNot(blUseNoLock) + .ToListAsync(); } /// @@ -179,11 +194,12 @@ namespace CoreCms.Net.Repository public List QueryListByClause(Expression> predicate, string orderBy = "", bool blUseNoLock = false) { - return blUseNoLock - ? DbBaseClient.Queryable().OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) - .WhereIF(predicate != null, predicate).With(SqlWith.NoLock).ToList() - : DbBaseClient.Queryable().OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) - .WhereIF(predicate != null, predicate).ToList(); + return DbBaseClient + .Queryable() + .OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) + .WhereIF(predicate != null, predicate) + .WithNoLockOrNot(blUseNoLock) + .ToList(); } /// @@ -196,11 +212,12 @@ namespace CoreCms.Net.Repository public async Task> QueryListByClauseAsync(Expression> predicate, string orderBy = "", bool blUseNoLock = false) { - return blUseNoLock - ? await DbBaseClient.Queryable().OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) - .WhereIF(predicate != null, predicate).With(SqlWith.NoLock).ToListAsync() - : await DbBaseClient.Queryable().OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) - .WhereIF(predicate != null, predicate).ToListAsync(); + return await DbBaseClient + .Queryable() + .OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) + .WhereIF(predicate != null, predicate) + .WithNoLockOrNot(blUseNoLock) + .ToListAsync(); } /// @@ -214,11 +231,12 @@ namespace CoreCms.Net.Repository public List QueryListByClause(Expression> predicate, Expression> orderByPredicate, OrderByType orderByType, bool blUseNoLock = false) { - return blUseNoLock - ? DbBaseClient.Queryable().OrderByIF(orderByPredicate != null, orderByPredicate, orderByType) - .WhereIF(predicate != null, predicate).With(SqlWith.NoLock).ToList() - : DbBaseClient.Queryable().OrderByIF(orderByPredicate != null, orderByPredicate, orderByType) - .WhereIF(predicate != null, predicate).ToList(); + return DbBaseClient + .Queryable() + .OrderByIF(orderByPredicate != null, orderByPredicate, orderByType) + .WhereIF(predicate != null, predicate) + .WithNoLockOrNot(blUseNoLock) + .ToList(); } /// @@ -232,11 +250,12 @@ namespace CoreCms.Net.Repository public async Task> QueryListByClauseAsync(Expression> predicate, Expression> orderByPredicate, OrderByType orderByType, bool blUseNoLock = false) { - return blUseNoLock - ? await DbBaseClient.Queryable().OrderByIF(orderByPredicate != null, orderByPredicate, orderByType) - .WhereIF(predicate != null, predicate).With(SqlWith.NoLock).ToListAsync() - : await DbBaseClient.Queryable().OrderByIF(orderByPredicate != null, orderByPredicate, orderByType) - .WhereIF(predicate != null, predicate).ToListAsync(); + return await DbBaseClient + .Queryable() + .OrderByIF(orderByPredicate != null, orderByPredicate, orderByType) + .WhereIF(predicate != null, predicate) + .WithNoLockOrNot(blUseNoLock) + .ToListAsync(); } /// @@ -251,11 +270,13 @@ namespace CoreCms.Net.Repository public List QueryListByClause(Expression> predicate, int take, Expression> orderByPredicate, OrderByType orderByType, bool blUseNoLock = false) { - return blUseNoLock - ? DbBaseClient.Queryable().OrderByIF(orderByPredicate != null, orderByPredicate, orderByType) - .WhereIF(predicate != null, predicate).Take(take).With(SqlWith.NoLock).ToList() - : DbBaseClient.Queryable().OrderByIF(orderByPredicate != null, orderByPredicate, orderByType) - .WhereIF(predicate != null, predicate).Take(take).ToList(); + return DbBaseClient + .Queryable() + .OrderByIF(orderByPredicate != null, orderByPredicate, orderByType) + .WhereIF(predicate != null, predicate) + .Take(take) + .WithNoLockOrNot(blUseNoLock) + .ToList(); } /// @@ -270,11 +291,13 @@ namespace CoreCms.Net.Repository public async Task> QueryListByClauseAsync(Expression> predicate, int take, Expression> orderByPredicate, OrderByType orderByType, bool blUseNoLock = false) { - return blUseNoLock - ? await DbBaseClient.Queryable().OrderByIF(orderByPredicate != null, orderByPredicate, orderByType) - .WhereIF(predicate != null, predicate).Take(take).With(SqlWith.NoLock).ToListAsync() - : await DbBaseClient.Queryable().OrderByIF(orderByPredicate != null, orderByPredicate, orderByType) - .WhereIF(predicate != null, predicate).Take(take).ToListAsync(); + return await DbBaseClient + .Queryable() + .OrderByIF(orderByPredicate != null, orderByPredicate, orderByType) + .WhereIF(predicate != null, predicate) + .Take(take) + .WithNoLockOrNot(blUseNoLock) + .ToListAsync(); } /// @@ -288,11 +311,13 @@ namespace CoreCms.Net.Repository public List QueryListByClause(Expression> predicate, int take, string strOrderByFileds = "", bool blUseNoLock = false) { - return blUseNoLock - ? DbBaseClient.Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds) - .Where(predicate).Take(take).With(SqlWith.NoLock).ToList() - : DbBaseClient.Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds) - .Where(predicate).Take(take).ToList(); + return DbBaseClient + .Queryable() + .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds) + .Where(predicate) + .Take(take) + .WithNoLockOrNot(blUseNoLock) + .ToList(); } /// @@ -306,11 +331,13 @@ namespace CoreCms.Net.Repository public async Task> QueryListByClauseAsync(Expression> predicate, int take, string strOrderByFileds = "", bool blUseNoLock = false) { - return blUseNoLock - ? await DbBaseClient.Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds) - .Where(predicate).Take(take).With(SqlWith.NoLock).ToListAsync() - : await DbBaseClient.Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds) - .Where(predicate).Take(take).ToListAsync(); + return await DbBaseClient + .Queryable() + .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds) + .Where(predicate) + .Take(take) + .WithNoLockOrNot(blUseNoLock) + .ToListAsync(); } /// @@ -321,9 +348,10 @@ namespace CoreCms.Net.Repository /// public T QueryByClause(Expression> predicate, bool blUseNoLock = false) { - return blUseNoLock - ? DbBaseClient.Queryable().With(SqlWith.NoLock).First(predicate) - : DbBaseClient.Queryable().First(predicate); + return DbBaseClient + .Queryable() + .WithNoLockOrNot(blUseNoLock) + .First(predicate); } /// @@ -334,9 +362,10 @@ namespace CoreCms.Net.Repository /// public async Task QueryByClauseAsync(Expression> predicate, bool blUseNoLock = false) { - return blUseNoLock - ? await DbBaseClient.Queryable().With(SqlWith.NoLock).FirstAsync(predicate) - : await DbBaseClient.Queryable().FirstAsync(predicate); + return await DbBaseClient + .Queryable() + .WithNoLockOrNot(blUseNoLock) + .FirstAsync(predicate); } /// @@ -350,9 +379,11 @@ namespace CoreCms.Net.Repository public T QueryByClause(Expression> predicate, Expression> orderByPredicate, OrderByType orderByType, bool blUseNoLock = false) { - return blUseNoLock - ? DbBaseClient.Queryable().OrderBy(orderByPredicate, orderByType).With(SqlWith.NoLock).First(predicate) - : DbBaseClient.Queryable().OrderBy(orderByPredicate, orderByType).First(predicate); + return DbBaseClient + .Queryable() + .OrderBy(orderByPredicate, orderByType) + .WithNoLockOrNot(blUseNoLock) + .First(predicate); } /// @@ -366,10 +397,11 @@ namespace CoreCms.Net.Repository public async Task QueryByClauseAsync(Expression> predicate, Expression> orderByPredicate, OrderByType orderByType, bool blUseNoLock = false) { - return blUseNoLock - ? await DbBaseClient.Queryable().OrderBy(orderByPredicate, orderByType).With(SqlWith.NoLock) - .FirstAsync(predicate) - : await DbBaseClient.Queryable().OrderBy(orderByPredicate, orderByType).FirstAsync(predicate); + return await DbBaseClient + .Queryable() + .OrderBy(orderByPredicate, orderByType) + .WithNoLockOrNot(blUseNoLock) + .FirstAsync(predicate); } /// @@ -379,7 +411,9 @@ namespace CoreCms.Net.Repository /// public int Insert(T entity) { - return DbBaseClient.Insertable(entity).ExecuteReturnIdentity(); + return DbBaseClient + .Insertable(entity) + .ExecuteReturnIdentity(); } /// @@ -389,7 +423,9 @@ namespace CoreCms.Net.Repository /// public async Task InsertAsync(T entity) { - return await DbBaseClient.Insertable(entity).ExecuteReturnIdentityAsync(); + return await DbBaseClient + .Insertable(entity) + .ExecuteReturnIdentityAsync(); } /// @@ -875,9 +911,7 @@ namespace CoreCms.Net.Repository /// public bool Exists(Expression> predicate, bool blUseNoLock = false) { - return blUseNoLock - ? DbBaseClient.Queryable().Where(predicate).With(SqlWith.NoLock).Any() - : DbBaseClient.Queryable().Where(predicate).Any(); + return DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).Any(); } /// @@ -888,9 +922,7 @@ namespace CoreCms.Net.Repository /// public async Task ExistsAsync(Expression> predicate, bool blUseNoLock = false) { - return blUseNoLock - ? await DbBaseClient.Queryable().Where(predicate).With(SqlWith.NoLock).AnyAsync() - : await DbBaseClient.Queryable().Where(predicate).AnyAsync(); + return await DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).AnyAsync(); } /// @@ -901,9 +933,7 @@ namespace CoreCms.Net.Repository /// public int GetCount(Expression> predicate, bool blUseNoLock = false) { - return blUseNoLock - ? DbBaseClient.Queryable().With(SqlWith.NoLock).Count(predicate) - : DbBaseClient.Queryable().Count(predicate); + return DbBaseClient.Queryable().WithNoLockOrNot(blUseNoLock).Count(predicate); } /// @@ -914,9 +944,7 @@ namespace CoreCms.Net.Repository /// public async Task GetCountAsync(Expression> predicate, bool blUseNoLock = false) { - return blUseNoLock - ? await DbBaseClient.Queryable().With(SqlWith.NoLock).CountAsync(predicate) - : await DbBaseClient.Queryable().CountAsync(predicate); + return await DbBaseClient.Queryable().WithNoLockOrNot(blUseNoLock).CountAsync(predicate); } /// @@ -928,9 +956,7 @@ namespace CoreCms.Net.Repository /// public int GetSum(Expression> predicate, Expression> field, bool blUseNoLock = false) { - return blUseNoLock - ? DbBaseClient.Queryable().Where(predicate).With(SqlWith.NoLock).Sum(field) - : DbBaseClient.Queryable().Where(predicate).Sum(field); + return DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).Sum(field); } /// @@ -943,9 +969,7 @@ namespace CoreCms.Net.Repository public async Task GetSumAsync(Expression> predicate, Expression> field, bool blUseNoLock = false) { - return blUseNoLock - ? await DbBaseClient.Queryable().Where(predicate).With(SqlWith.NoLock).SumAsync(field) - : await DbBaseClient.Queryable().Where(predicate).SumAsync(field); + return await DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).SumAsync(field); } /// @@ -958,9 +982,7 @@ namespace CoreCms.Net.Repository public decimal GetSum(Expression> predicate, Expression> field, bool blUseNoLock = false) { - return blUseNoLock - ? DbBaseClient.Queryable().Where(predicate).With(SqlWith.NoLock).Sum(field) - : DbBaseClient.Queryable().Where(predicate).Sum(field); + return DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).Sum(field); } /// @@ -973,9 +995,7 @@ namespace CoreCms.Net.Repository public async Task GetSumAsync(Expression> predicate, Expression> field, bool blUseNoLock = false) { - return blUseNoLock - ? await DbBaseClient.Queryable().Where(predicate).With(SqlWith.NoLock).SumAsync(field) - : await DbBaseClient.Queryable().Where(predicate).SumAsync(field); + return await DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).SumAsync(field); } /// @@ -988,9 +1008,7 @@ namespace CoreCms.Net.Repository public float GetSum(Expression> predicate, Expression> field, bool blUseNoLock = false) { - return blUseNoLock - ? DbBaseClient.Queryable().Where(predicate).With(SqlWith.NoLock).Sum(field) - : DbBaseClient.Queryable().Where(predicate).Sum(field); + return DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).Sum(field); } /// @@ -1003,9 +1021,7 @@ namespace CoreCms.Net.Repository public async Task GetSumAsync(Expression> predicate, Expression> field, bool blUseNoLock = false) { - return blUseNoLock - ? await DbBaseClient.Queryable().Where(predicate).With(SqlWith.NoLock).SumAsync(field) - : await DbBaseClient.Queryable().Where(predicate).SumAsync(field); + return await DbBaseClient.Queryable().Where(predicate).WithNoLockOrNot(blUseNoLock).SumAsync(field); } /// @@ -1021,12 +1037,12 @@ namespace CoreCms.Net.Repository int pageSize = 20, bool blUseNoLock = false) { var totalCount = 0; - var page = blUseNoLock - ? DbBaseClient.Queryable().OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) - .WhereIF(predicate != null, predicate).With(SqlWith.NoLock) - .ToPageList(pageIndex, pageSize, ref totalCount) - : DbClient.Queryable().OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) - .WhereIF(predicate != null, predicate).ToPageList(pageIndex, pageSize, ref totalCount); + 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; @@ -1045,12 +1061,12 @@ namespace CoreCms.Net.Repository int pageIndex = 1, int pageSize = 20, bool blUseNoLock = false) { RefAsync totalCount = 0; - var page = blUseNoLock - ? await DbBaseClient.Queryable().OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) - .WhereIF(predicate != null, predicate).With(SqlWith.NoLock) - .ToPageListAsync(pageIndex, pageSize, totalCount) - : await DbBaseClient.Queryable().OrderByIF(!string.IsNullOrEmpty(orderBy), orderBy) - .WhereIF(predicate != null, predicate).ToPageListAsync(pageIndex, pageSize, totalCount); + 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; } @@ -1070,12 +1086,13 @@ namespace CoreCms.Net.Repository int pageSize = 20, bool blUseNoLock = false) { var totalCount = 0; - var page = blUseNoLock - ? DbBaseClient.Queryable().OrderByIF(orderByExpression != null, orderByExpression, orderByType) - .WhereIF(predicate != null, predicate).With(SqlWith.NoLock) - .ToPageList(pageIndex, pageSize, ref totalCount) - : DbBaseClient.Queryable().OrderByIF(orderByExpression != null, orderByExpression, orderByType) - .WhereIF(predicate != null, predicate).ToPageList(pageIndex, pageSize, ref totalCount); + 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; } @@ -1095,11 +1112,13 @@ namespace CoreCms.Net.Repository int pageSize = 20, bool blUseNoLock = false) { RefAsync totalCount = 0; - var page = blUseNoLock - ? await DbBaseClient.Queryable().WhereIF(predicate != null, predicate).OrderByIF(orderByExpression != null, orderByExpression, orderByType) - .With(SqlWith.NoLock).ToPageListAsync(pageIndex, pageSize, totalCount) - : await DbBaseClient.Queryable().WhereIF(predicate != null, predicate).OrderByIF(orderByExpression != null, orderByExpression, orderByType) - .ToPageListAsync(pageIndex, pageSize, totalCount); + 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; } @@ -1121,14 +1140,12 @@ namespace CoreCms.Net.Repository Expression> whereLambda = null, bool blUseNoLock = false) where T1 : class, new() { - if (whereLambda == null) - return blUseNoLock - ? DbBaseClient.Queryable(joinExpression).Select(selectExpression).With(SqlWith.NoLock).ToList() - : DbBaseClient.Queryable(joinExpression).Select(selectExpression).ToList(); - return blUseNoLock - ? DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).With(SqlWith.NoLock) - .ToList() - : DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).ToList(); + return DbBaseClient + .Queryable(joinExpression) + .WhereIF(whereLambda is not null, whereLambda) + .Select(selectExpression) + .WithNoLockOrNot(blUseNoLock) + .ToList(); } /// @@ -1148,15 +1165,12 @@ namespace CoreCms.Net.Repository Expression> whereLambda = null, bool blUseNoLock = false) where T1 : class, new() { - if (whereLambda == null) - return blUseNoLock - ? await DbBaseClient.Queryable(joinExpression).Select(selectExpression).With(SqlWith.NoLock) - .ToListAsync() - : await DbBaseClient.Queryable(joinExpression).Select(selectExpression).ToListAsync(); - return blUseNoLock - ? await DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression) - .With(SqlWith.NoLock).ToListAsync() - : await DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).ToListAsync(); + return await DbBaseClient + .Queryable(joinExpression) + .WhereIF(whereLambda is not null, whereLambda) + .Select(selectExpression) + .WithNoLockOrNot(blUseNoLock) + .ToListAsync(); } /// @@ -1176,14 +1190,12 @@ namespace CoreCms.Net.Repository Expression> whereLambda = null, bool blUseNoLock = false) where T1 : class, new() { - if (whereLambda == null) - return blUseNoLock - ? DbBaseClient.Queryable(joinExpression).Select(selectExpression).With(SqlWith.NoLock).First() - : DbBaseClient.Queryable(joinExpression).Select(selectExpression).First(); - return blUseNoLock - ? DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).With(SqlWith.NoLock) - .First() - : DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).First(); + return DbBaseClient + .Queryable(joinExpression) + .WhereIF(whereLambda is not null, whereLambda) + .Select(selectExpression) + .WithNoLockOrNot(blUseNoLock) + .First(); } /// @@ -1203,15 +1215,11 @@ namespace CoreCms.Net.Repository Expression> whereLambda = null, bool blUseNoLock = false) where T1 : class, new() { - if (whereLambda == null) - return blUseNoLock - ? await DbBaseClient.Queryable(joinExpression).Select(selectExpression).With(SqlWith.NoLock) - .FirstAsync() - : await DbBaseClient.Queryable(joinExpression).Select(selectExpression).FirstAsync(); - return blUseNoLock - ? await DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression) - .With(SqlWith.NoLock).FirstAsync() - : await DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).FirstAsync(); + return await DbBaseClient + .Queryable(joinExpression) + .WhereIF(whereLambda is not null, whereLambda) + .Select(selectExpression).WithNoLockOrNot(blUseNoLock) + .FirstAsync(); } /// @@ -1232,16 +1240,12 @@ namespace CoreCms.Net.Repository Expression> whereLambda = null, bool blUseNoLock = false) where T1 : class, new() { - if (whereLambda == null) - return blUseNoLock - ? DbBaseClient.Queryable(joinExpression).Select(selectExpression).With(SqlWith.NoLock) - .ToList() - : DbBaseClient.Queryable(joinExpression).Select(selectExpression).ToList(); - return blUseNoLock - ? DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).With(SqlWith.NoLock) - .ToList() - : DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression) - .ToList(); + return DbBaseClient + .Queryable(joinExpression) + .WhereIF(whereLambda is not null, whereLambda) + .Select(selectExpression) + .WithNoLockOrNot(blUseNoLock) + .ToList(); } /// @@ -1262,17 +1266,12 @@ namespace CoreCms.Net.Repository Expression> whereLambda = null, bool blUseNoLock = false) where T1 : class, new() { - if (whereLambda == null) - return blUseNoLock - ? await DbBaseClient.Queryable(joinExpression).Select(selectExpression).With(SqlWith.NoLock) - .ToListAsync() - : await DbBaseClient.Queryable(joinExpression).Select(selectExpression).ToListAsync(); - return blUseNoLock - ? await DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression) - .With(SqlWith.NoLock) - .ToListAsync() - : await DbBaseClient.Queryable(joinExpression).Where(whereLambda).Select(selectExpression) - .ToListAsync(); + return await DbBaseClient + .Queryable(joinExpression) + .WhereIF(whereLambda is not null, whereLambda) + .Select(selectExpression) + .WithNoLockOrNot(blUseNoLock) + .ToListAsync(); } /// diff --git a/CoreCms.Net.Repository/SqlSugarExtensions.cs b/CoreCms.Net.Repository/SqlSugarExtensions.cs new file mode 100644 index 0000000..49ffb19 --- /dev/null +++ b/CoreCms.Net.Repository/SqlSugarExtensions.cs @@ -0,0 +1,17 @@ +using SqlSugar; + +namespace CoreCms.Net.Repository +{ + internal static class SqlSugarExtensions + { + internal static ISugarQueryable WithNoLockOrNot(this ISugarQueryable query,bool @lock = false) + { + if (@lock) + { + query = query.With(SqlWith.NoLock); + } + + return query; + } + } +} -- Gitee