-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
IQueryableExtensions.cs
47 lines (43 loc) · 1.89 KB
/
IQueryableExtensions.cs
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
40
41
42
43
44
45
46
47
// MIT License Copyright 2019 (c) David Melendez. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ElCamino.AspNetCore.Identity.CosmosDB.Extensions
{
internal static class IQueryableExtensions
{
public static Task<List<TSource>> ToListAsync<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> where = null, CancellationToken cancellationToken = default(CancellationToken))
{
if (where == null)
{
List<TSource> list = source as List<TSource>;
if (list != null)
{
return Task.FromResult(list);
}
return Task.Run(() => { return source.ToList(); }, cancellationToken);
}
else
return Task.Run(() => { return source.Where(where).ToList(); }, cancellationToken);
}
public static Task<TSource> FirstOrDefaultAsync<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> where = null, CancellationToken cancellationToken = default(CancellationToken))
{
return Task.Run(() => {
if (where == null)
return source.FirstOrDefault();
return source.FirstOrDefault(where);
}, cancellationToken);
}
public static Task<TSource> SingleOrDefaultAsync<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> where = null, CancellationToken cancellationToken = default(CancellationToken))
{
return Task.Run(() => {
if(where == null)
return source.SingleOrDefault();
return source.Where(where).SingleOrDefault();
}, cancellationToken);
}
}
}