| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | using Elsa.Common.Models; |
| | | 3 | | using JetBrains.Annotations; |
| | | 4 | | using Microsoft.EntityFrameworkCore; |
| | | 5 | | |
| | | 6 | | // ReSharper disable once CheckNamespace |
| | | 7 | | namespace Elsa.Persistence.EFCore.Extensions; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Provides extensions to <see cref="IQueryable{T}"/>. |
| | | 11 | | /// </summary> |
| | | 12 | | [PublicAPI] |
| | | 13 | | public static class QueryableExtensions |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Inserts a list of entities in bulk. |
| | | 17 | | /// </summary> |
| | | 18 | | public static async Task BulkInsertAsync<TDbContext, TEntity>(this TDbContext dbContext, IList<TEntity> entities, Ca |
| | | 19 | | { |
| | 323 | 20 | | var set = dbContext.Set<TEntity>(); |
| | | 21 | | |
| | 323 | 22 | | if (entities.Any()) |
| | 323 | 23 | | await set.AddRangeAsync(entities, cancellationToken); |
| | | 24 | | |
| | 323 | 25 | | await dbContext.SaveChangesAsync(cancellationToken); |
| | 323 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Returns a paged result from the specified query. |
| | | 30 | | /// </summary> |
| | | 31 | | public static async Task<Page<TTarget>> PaginateAsync<T, TTarget>(this IQueryable<T> queryable, Expression<Func<T, T |
| | | 32 | | { |
| | 0 | 33 | | var count = await queryable.CountAsync(); |
| | 0 | 34 | | if (pageArgs?.Offset != null) queryable = queryable.Skip(pageArgs.Offset.Value); |
| | 0 | 35 | | if (pageArgs?.Limit != null) queryable = queryable.Take(pageArgs.Limit.Value); |
| | 0 | 36 | | var results = await queryable.Select(projection).ToListAsync(); |
| | 0 | 37 | | return Page.Of(results, count); |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Returns a paged result from the specified query. |
| | | 42 | | /// </summary> |
| | | 43 | | public static async Task<Page<T>> PaginateAsync<T>(this IQueryable<T> queryable, PageArgs? pageArgs = null) |
| | | 44 | | { |
| | 0 | 45 | | var count = await queryable.CountAsync(); |
| | 0 | 46 | | if (pageArgs?.Offset != null) queryable = queryable.Skip(pageArgs.Offset.Value); |
| | 0 | 47 | | if (pageArgs?.Limit != null) queryable = queryable.Take(pageArgs.Limit.Value); |
| | 0 | 48 | | var results = await queryable.ToListAsync(); |
| | 0 | 49 | | return Page.Of(results, count); |
| | 0 | 50 | | } |
| | | 51 | | } |