Fauna v10 .NET/C# Driver 1.0.1
 
Loading...
Searching...
No Matches
QuerySourceToCollections.cs
Go to the documentation of this file.
1using System.Collections;
2
3namespace Fauna.Linq;
4
9public partial class QuerySource<T>
10{
15 public List<T> ToList() => ToEnumerable().ToList();
16
21 public async Task<List<T>> ToListAsync(CancellationToken cancel = default)
22 {
23 var ret = new List<T>();
24 await foreach (var e in ToAsyncEnumerable(cancel)) ret.Add(e);
25 return ret;
26 }
27
32 public T[] ToArray() => ToEnumerable().ToArray();
33
38 public async Task<T[]> ToArrayAsync(CancellationToken cancel = default) =>
39 (await ToListAsync(cancel)).ToArray();
40
45 public HashSet<T> ToHashSet() => ToHashSet(null);
46
51 public Task<HashSet<T>> ToHashSetAsync(CancellationToken cancel = default) =>
52 ToHashSetAsync(null, cancel);
53
59 public HashSet<T> ToHashSet(IEqualityComparer<T>? comparer) => ToEnumerable().ToHashSet(comparer);
60
67 public async Task<HashSet<T>> ToHashSetAsync(IEqualityComparer<T>? comparer, CancellationToken cancel = default)
68 {
69 var ret = new HashSet<T>(comparer);
70 await foreach (var e in ToAsyncEnumerable(cancel)) ret.Add(e);
71 return ret;
72 }
73
82 public Dictionary<K, V> ToDictionary<K, V>(Func<T, K> getKey, Func<T, V> getValue) where K : notnull =>
83 ToDictionary(getKey, getValue, null);
84
94 public Task<Dictionary<K, V>> ToDictionaryAsync<K, V>(Func<T, K> getKey, Func<T, V> getValue, CancellationToken cancel = default) where K : notnull =>
95 ToDictionaryAsync(getKey, getValue, null, cancel);
96
106 public Dictionary<K, V> ToDictionary<K, V>(Func<T, K> getKey, Func<T, V> getValue, IEqualityComparer<K>? comparer) where K : notnull =>
107 ToEnumerable().ToDictionary(getKey, getValue, comparer);
108
119 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
120 {
121 var ret = new Dictionary<K, V>(comparer);
122 await foreach (var e in ToAsyncEnumerable(cancel)) ret[getKey(e)] = getValue(e);
123 return ret;
124 }
125
130 public record struct QuerySourceEnumerable(QuerySource<T> Source) : IEnumerable<T>
131 {
136 public IEnumerator<T> GetEnumerator()
137 {
138 var pe = Source.PaginateAsync().GetAsyncEnumerator();
139 try
140 {
141 var mv = pe.MoveNextAsync().AsTask();
142 mv.Wait();
143 while (mv.Result)
144 {
145 var page = pe.Current;
146
147 foreach (var e in page.Data)
148 {
149 yield return e;
150 }
151
152 mv = pe.MoveNextAsync().AsTask();
153 mv.Wait();
154 }
155 }
156 finally { pe.DisposeAsync(); }
157 }
158
159 IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
160 }
161}
An abstract class representing a QuerySource for LINQ-style queries.
async Task< HashSet< T > > ToHashSetAsync(IEqualityComparer< T >? comparer, CancellationToken cancel=default)
Executes the query asynchronously and converts the results to a HashSet<T>.
HashSet< T > ToHashSet()
Executes the query synchronously and converts the results to a HashSet<T>.
IEnumerable< T > ToEnumerable()
Executes a query.
T[] ToArray()
Executes the query synchronously and converts the results to a T:T[].
Task< Dictionary< K, V > > ToDictionaryAsync< K, V >(Func< T, K > getKey, Func< T, V > getValue, CancellationToken cancel=default)
Executes the query asynchronously and converts the results to a Dictionary<K,V>.
Task< HashSet< T > > ToHashSetAsync(CancellationToken cancel=default)
Executes the query asynchronously and converts the results to a HashSet<T>.
HashSet< T > ToHashSet(IEqualityComparer< T >? comparer)
Executes the query synchronously and converts the results to a HashSet<T> using a comparer.
async Task< T[]> ToArrayAsync(CancellationToken cancel=default)
Executes the query asynchronously and converts the results to a T:T[].
List< T > ToList()
Executes the query synchronously and converts the results to a List<T>.
Dictionary< K, V > ToDictionary< K, V >(Func< T, K > getKey, Func< T, V > getValue)
Executes the query synchronously and converts the results to a Dictionary<K,V>.
async Task< List< T > > ToListAsync(CancellationToken cancel=default)
Executes the query asynchronously and converts the results to a List<T>.
IAsyncEnumerable< T > ToAsyncEnumerable(CancellationToken cancel=default)
Executes a query asynchronously.