-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsyncQueryable.cs
106 lines (89 loc) · 2.38 KB
/
AsyncQueryable.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Query.Internal;
namespace Foo
{
public class AsyncQueryable<T> : EnumerableQuery<T>, IQueryable<T>, IAsyncEnumerable<T>
{
public AsyncQueryable(IEnumerable<T> enumerable)
: base(enumerable)
{
}
public AsyncQueryable(Expression expression)
: base(expression)
{
}
public IAsyncEnumerator<T> GetEnumerator()
{
return new AsyncQueryableEnumerator<T>(this.AsEnumerable().GetEnumerator());
}
IQueryProvider IQueryable.Provider => new EfAsyncQueryProvider<T>(this);
}
public class AsyncQueryableEnumerator<T> : IAsyncEnumerator<T>
{
private readonly IEnumerator<T> _source;
public T Current => _source.Current;
public AsyncQueryableEnumerator(IEnumerator<T> source)
{
_source = source;
}
public void Dispose()
{
_source.Dispose();
}
public Task<bool> MoveNext(CancellationToken cancellationToken)
{
try
{
return Task.FromResult(_source.MoveNext());
}
catch (Exception e)
{
return Task.FromResult(false);
}
}
}
public class EfAsyncQueryProvider<T> : IAsyncQueryProvider
{
private readonly IQueryProvider _source;
public EfAsyncQueryProvider(IQueryProvider source)
{
_source = source;
}
public IQueryable CreateQuery(Expression expression)
{
return new AsyncQueryable<T>(expression);
}
public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
{
return new AsyncQueryable<TElement>(expression);
}
public object Execute(Expression expression)
{
return _source.Execute(expression);
}
public TResult Execute<TResult>(Expression expression)
{
return _source.Execute<TResult>(expression);
}
public IAsyncEnumerable<TResult> ExecuteAsync<TResult>(Expression expression)
{
return new AsyncQueryable<TResult>(expression);
}
public Task<TResult> ExecuteAsync<TResult>(Expression expression, CancellationToken cancellationToken)
{
return Task.FromResult(Execute<TResult>(expression));
}
}
public static class IEnumerableExtensions
{
public static IQueryable<T> AsAsyncQueryable<T>(this IEnumerable<T> source)
{
return new AsyncQueryable<T>(source);
}
}
}