Fauna v10 .NET/C# Driver 0.2.0-beta
 
Loading...
Searching...
No Matches
QuerySourceToCollections.cs
Go to the documentation of this file.
1using System.Collections;
2
3namespace Fauna.Linq;
4
5public partial class QuerySource<T>
6{
7 public List<T> ToList() => ToEnumerable().ToList();
8 public async Task<List<T>> ToListAsync(CancellationToken cancel = default)
9 {
10 var ret = new List<T>();
11 await foreach (var e in ToAsyncEnumerable(cancel)) ret.Add(e);
12 return ret;
13 }
14
15 public T[] ToArray() => ToEnumerable().ToArray();
16 public async Task<T[]> ToArrayAsync(CancellationToken cancel = default) =>
17 (await ToListAsync(cancel)).ToArray();
18
19 public HashSet<T> ToHashSet() => ToHashSet(null);
20 public Task<HashSet<T>> ToHashSetAsync(CancellationToken cancel = default) =>
21 ToHashSetAsync(null, cancel);
22
23 public HashSet<T> ToHashSet(IEqualityComparer<T>? comparer) => ToEnumerable().ToHashSet(comparer);
24 public async Task<HashSet<T>> ToHashSetAsync(IEqualityComparer<T>? comparer, CancellationToken cancel = default)
25 {
26 var ret = new HashSet<T>(comparer);
27 await foreach (var e in ToAsyncEnumerable(cancel)) ret.Add(e);
28 return ret;
29 }
30
31 public Dictionary<K, V> ToDictionary<K, V>(Func<T, K> getKey, Func<T, V> getValue) where K : notnull =>
32 ToDictionary(getKey, getValue, null);
33 public Task<Dictionary<K, V>> ToDictionaryAsync<K, V>(Func<T, K> getKey, Func<T, V> getValue, CancellationToken cancel = default) where K : notnull =>
34 ToDictionaryAsync(getKey, getValue, null, cancel);
35
36 public Dictionary<K, V> ToDictionary<K, V>(Func<T, K> getKey, Func<T, V> getValue, IEqualityComparer<K>? comparer) where K : notnull =>
37 ToEnumerable().ToDictionary(getKey, getValue, comparer);
38 public async Task<Dictionary<K, V>> ToDictionaryAsync<K, V>(Func<T, K> getKey, Func<T, V> getValue, IEqualityComparer<K>? comparer, CancellationToken cancel = default) where K : notnull
39 {
40 var ret = new Dictionary<K, V>(comparer);
41 await foreach (var e in ToAsyncEnumerable(cancel)) ret[getKey(e)] = getValue(e);
42 return ret;
43 }
44
45 public record struct QuerySourceEnumerable(QuerySource<T> Source) : IEnumerable<T>
46 {
47 public IEnumerator<T> GetEnumerator()
48 {
49 var pe = Source.PaginateAsync().GetAsyncEnumerator();
50 try
51 {
52 var mv = pe.MoveNextAsync().AsTask();
53 mv.Wait();
54 while (mv.Result)
55 {
56 var page = pe.Current;
57
58 foreach (var e in page.Data)
59 {
60 yield return e;
61 }
62
63 mv = pe.MoveNextAsync().AsTask();
64 mv.Wait();
65 }
66 }
67 finally { pe.DisposeAsync(); }
68 }
69
70 IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
71 }
72}
async Task< HashSet< T > > ToHashSetAsync(IEqualityComparer< T >? comparer, CancellationToken cancel=default)
HashSet< T > ToHashSet()
IEnumerable< T > ToEnumerable()
Task< Dictionary< K, V > > ToDictionaryAsync< K, V >(Func< T, K > getKey, Func< T, V > getValue, CancellationToken cancel=default)
Task< HashSet< T > > ToHashSetAsync(CancellationToken cancel=default)
HashSet< T > ToHashSet(IEqualityComparer< T >? comparer)
async Task< T[]> ToArrayAsync(CancellationToken cancel=default)
Dictionary< K, V > ToDictionary< K, V >(Func< T, K > getKey, Func< T, V > getValue)
async Task< List< T > > ToListAsync(CancellationToken cancel=default)
IAsyncEnumerable< T > ToAsyncEnumerable(CancellationToken cancel=default)