Browse Source

Merge pull request #12621 from abpframework/OrderByIf

Add `OrderByIf` extension method.
pull/12921/head
liangshiwei 4 years ago
committed by GitHub
parent
commit
6cd1e05fdc
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      framework/src/Volo.Abp.Core/System/Linq/AbpQueryableExtensions.cs
  2. 2
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs
  3. 2
      framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs
  4. 2
      framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs

17
framework/src/Volo.Abp.Core/System/Linq/AbpQueryableExtensions.cs

@ -95,4 +95,21 @@ public static class AbpQueryableExtensions
? (TQueryable)query.Where(predicate) ? (TQueryable)query.Where(predicate)
: query; : query;
} }
/// <summary>
/// Order a <see cref="IQueryable{T}"/> by given predicate if given condition is true.
/// </summary>
/// <param name="query">Queryable to apply filtering</param>
/// <param name="condition">A boolean value</param>
/// <param name="sorting">Order the query</param>
/// <returns>Order or not order query based on <paramref name="condition"/></returns>
public static TQueryable OrderByIf<T, TQueryable>([NotNull] this TQueryable query, bool condition, string sorting)
where TQueryable : IQueryable<T>
{
Check.NotNull(query, nameof(query));
return condition
? (TQueryable)Dynamic.Core.DynamicQueryableExtensions.OrderBy(query, sorting)
: query;
}
} }

2
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs

@ -252,7 +252,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
: await GetDbSetAsync(); : await GetDbSetAsync();
return await queryable return await queryable
.OrderBy(sorting) .OrderByIf<TEntity, IQueryable<TEntity>>(!sorting.IsNullOrWhiteSpace(), sorting)
.PageBy(skipCount, maxResultCount) .PageBy(skipCount, maxResultCount)
.ToListAsync(GetCancellationToken(cancellationToken)); .ToListAsync(GetCancellationToken(cancellationToken));
} }

2
framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs

@ -285,7 +285,7 @@ public class MemoryDbRepository<TMemoryDbContext, TEntity> : RepositoryBase<TEnt
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
return (await GetQueryableAsync()) return (await GetQueryableAsync())
.OrderBy(sorting) .OrderByIf<TEntity, IQueryable<TEntity>>(!sorting.IsNullOrWhiteSpace(), sorting)
.PageBy(skipCount, maxResultCount) .PageBy(skipCount, maxResultCount)
.ToList(); .ToList();
} }

2
framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs

@ -465,7 +465,7 @@ public class MongoDbRepository<TMongoDbContext, TEntity>
cancellationToken = GetCancellationToken(cancellationToken); cancellationToken = GetCancellationToken(cancellationToken);
return await (await GetMongoQueryableAsync(cancellationToken)) return await (await GetMongoQueryableAsync(cancellationToken))
.OrderBy(sorting) .OrderByIf<TEntity, IQueryable<TEntity>>(!sorting.IsNullOrWhiteSpace(), sorting)
.As<IMongoQueryable<TEntity>>() .As<IMongoQueryable<TEntity>>()
.PageBy<TEntity, IMongoQueryable<TEntity>>(skipCount, maxResultCount) .PageBy<TEntity, IMongoQueryable<TEntity>>(skipCount, maxResultCount)
.ToListAsync(cancellationToken); .ToListAsync(cancellationToken);

Loading…
Cancel
Save