generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathEfDbQueryableExtensions.cs
More file actions
39 lines (34 loc) · 1.85 KB
/
EfDbQueryableExtensions.cs
File metadata and controls
39 lines (34 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using CoreEx.Entities;
namespace Microsoft.EntityFrameworkCore
{
/// <summary>
/// Adds additional extension methods to <see cref="IQueryable{T}"/>.
/// </summary>
public static class EfDbQueryableExtensions
{
/// <summary>
/// Creates a <see cref="ICollectionResult"/> from a <see cref="IQueryable{TItem}"/>.
/// </summary>
/// <typeparam name="TCollResult">The <see cref="ICollectionResult"/> <see cref="Type"/>.</typeparam>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <typeparam name="TItem">The item <see cref="Type"/>.</typeparam>
/// <param name="query">The <see cref="IEnumerable{TItem}"/>.</param>
/// <param name="paging">The <see cref="PagingArgs"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>A new collection that contains the elements from the input sequence.</returns>
public static async Task<TCollResult> ToCollectionResultAsync<TCollResult, TColl, TItem>(this IQueryable<TItem> query, PagingArgs? paging = null, CancellationToken cancellationToken = default)
where TCollResult : ICollectionResult<TItem>, new()
where TColl : ICollection<TItem>, new()
{
var result = new TCollResult { Paging = new PagingResult(paging) };
await foreach (var item in query.WithPaging(paging).AsAsyncEnumerable().WithCancellation(cancellationToken).ConfigureAwait(false))
{
result.Items.Add(item);
}
if (result.Paging.IsGetCount)
result.Paging.TotalCount = await query.LongCountAsync(cancellationToken).ConfigureAwait(false);
return result;
}
}
}