Fauna v10 .NET/C# Driver 1.0.1
 
Loading...
Searching...
No Matches
IQuerySource.cs
Go to the documentation of this file.
1using System.Linq.Expressions;
2using Fauna.Core;
3using Fauna.Types;
4
5namespace Fauna.Linq;
6
10public interface IQuerySource
11{
12 // TODO(matt) use an API-specific exception in-line with what other LINQ
13 // libraries do.
14 internal static Exception Fail(Expression? expr) =>
15 Fail($"Unsupported {expr?.NodeType} expression: {expr}");
16
17 internal static Exception Fail(string op, string msg) =>
18 Fail($"Unsupported method call `{op}`: {msg}");
19
20 internal static Exception Fail(string msg) => new NotSupportedException(msg);
21}
22
27public interface IQuerySource<T> : IQuerySource
28{
29 // Core execution
30
37 public IAsyncEnumerable<Page<T>> PaginateAsync(QueryOptions? queryOptions = null, CancellationToken cancel = default);
43 public IAsyncEnumerable<T> ToAsyncEnumerable(CancellationToken cancel = default);
48 public IEnumerable<T> ToEnumerable();
49
50 // Composition methods
51
66 public IQuerySource<T> OrderBy<K>(Expression<Func<T, K>> keySelector);
76 public IQuerySource<T> OrderByDescending<K>(Expression<Func<T, K>> keySelector);
86 public IQuerySource<R> Select<R>(Expression<Func<T, R>> selector);
87 // public IQuerySource<R> SelectMany<R>(Expression<Func<T, IQuerySource<R>>> selector);
92 public IQuerySource<T> Skip(int count);
93 // public IQuerySource<R> SelectMany<R>(Expression<Func<T, IQuerySource<R>>> selector);
98 public IQuerySource<T> Take(int count);
99 // public IQuerySource<R> SelectMany<R>(Expression<Func<T, IQuerySource<R>>> selector);
104 public IQuerySource<T> Where(Expression<Func<T, bool>> predicate);
105
106 // Terminal result methods
107
108 // public R Aggregate<A, R>(A seed, Expression<Func<A, T, A>> accum, Func<A, R> selector);
109 // public Task<R> AggregateAsync<A, R>(A seed, Expression<Func<A, T, A>> accum, Func<A, R> selector);
110
111 // // not IQueryable
112 // public R Fold<R>(R seed, Expression<Func<R, T, R>> accum);
113 // public Task<R> FoldAsync<R>(R seed, Expression<Func<R, T, R>> accum);
114
115 // public IQuerySource<R> SelectMany<R>(Expression<Func<T, IQuerySource<R>>> selector);
116
121 public bool All(Expression<Func<T, bool>> predicate);
122
127 public Task<bool> AllAsync(Expression<Func<T, bool>> predicate, CancellationToken cancel = default);
128
133 public bool Any();
138 public Task<bool> AnyAsync(CancellationToken cancel = default);
139
144 public bool Any(Expression<Func<T, bool>> predicate);
145
146
151 public Task<bool> AnyAsync(Expression<Func<T, bool>> predicate, CancellationToken cancel = default);
152
157 public int Count();
162 public Task<int> CountAsync(CancellationToken cancel = default);
163
168 public int Count(Expression<Func<T, bool>> predicate);
173 public Task<int> CountAsync(Expression<Func<T, bool>> predicate, CancellationToken cancel = default);
174
179 public T First();
184 public Task<T> FirstAsync(CancellationToken cancel = default);
185
190 public T First(Expression<Func<T, bool>> predicate);
196 public Task<T> FirstAsync(Expression<Func<T, bool>> predicate, CancellationToken cancel = default);
197
203 public T? FirstOrDefault();
209 public Task<T?> FirstOrDefaultAsync(CancellationToken cancel = default);
210
216 public T? FirstOrDefault(Expression<Func<T, bool>> predicate);
222 public Task<T?> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate, CancellationToken cancel = default);
223
224
229 public T Last();
234 public Task<T> LastAsync(CancellationToken cancel = default);
235
241 public T Last(Expression<Func<T, bool>> predicate);
247 public Task<T> LastAsync(Expression<Func<T, bool>> predicate, CancellationToken cancel = default);
248
254 public T? LastOrDefault();
260 public Task<T?> LastOrDefaultAsync(CancellationToken cancel = default);
261
267 public T? LastOrDefault(Expression<Func<T, bool>> predicate);
273 public Task<T?> LastOrDefaultAsync(Expression<Func<T, bool>> predicate, CancellationToken cancel = default);
274
279 public long LongCount();
284 public Task<long> LongCountAsync(CancellationToken cancel = default);
285
290 public long LongCount(Expression<Func<T, bool>> predicate);
295 public Task<long> LongCountAsync(Expression<Func<T, bool>> predicate, CancellationToken cancel = default);
296
297
304 public T Max();
311 public Task<T> MaxAsync(CancellationToken cancel = default);
312
319 public R Max<R>(Expression<Func<T, R>> selector);
327 public Task<R> MaxAsync<R>(Expression<Func<T, R>> selector, CancellationToken cancel = default);
328
329 // public T MaxBy<K>(Expression<Func<T, K>> selector);
330 // public Task<K> MaxByAsync<K>(Expression<Func<T, K>> selector, CancellationToken cancel = default);
331
338 public T Min();
345 public Task<T> MinAsync(CancellationToken cancel = default);
346
347 // public T MinBy<K>(Expression<Func<T, K>> selector);
348 // public Task<K> MinByAsync<K>(Expression<Func<T, K>> selector, CancellationToken cancel = default);
349
357 public R Min<R>(Expression<Func<T, R>> selector);
365 public Task<R> MinAsync<R>(Expression<Func<T, R>> selector, CancellationToken cancel = default);
366
372 public T Single();
378 public Task<T> SingleAsync(CancellationToken cancel = default);
379
385 public T Single(Expression<Func<T, bool>> predicate);
391 public Task<T> SingleAsync(Expression<Func<T, bool>> predicate, CancellationToken cancel = default);
392
398 public T SingleOrDefault();
404 public Task<T> SingleOrDefaultAsync(CancellationToken cancel = default);
405
411 public T SingleOrDefault(Expression<Func<T, bool>> predicate);
417 public Task<T> SingleOrDefaultAsync(Expression<Func<T, bool>> predicate, CancellationToken cancel = default);
418
424 public int Sum(Expression<Func<T, int>> selector);
430 public Task<int> SumAsync(Expression<Func<T, int>> selector, CancellationToken cancel = default);
431
437 public long Sum(Expression<Func<T, long>> selector);
443 public Task<long> SumAsync(Expression<Func<T, long>> selector, CancellationToken cancel = default);
444
445 // public float Sum(Expression<Func<T, float>> selector);
446 // public Task<float> SumAsync(Expression<Func<T, float>> selector, CancellationToken cancel = default);
447
453 public double Sum(Expression<Func<T, double>> selector);
459 public Task<double> SumAsync(Expression<Func<T, double>> selector, CancellationToken cancel = default);
460
466 public double Average(Expression<Func<T, double>> selector);
472 public Task<double> AverageAsync(Expression<Func<T, double>> selector, CancellationToken cancel = default);
473
474 // Collection result methods
475
480 public List<T> ToList();
485 public Task<List<T>> ToListAsync(CancellationToken cancel = default);
486
491 public T[] ToArray();
496 public Task<T[]> ToArrayAsync(CancellationToken cancel = default);
497
502 public HashSet<T> ToHashSet();
507 public Task<HashSet<T>> ToHashSetAsync(CancellationToken cancel = default);
508
509
515 public HashSet<T> ToHashSet(IEqualityComparer<T>? comparer);
516
523 public Task<HashSet<T>> ToHashSetAsync(IEqualityComparer<T>? comparer, CancellationToken cancel = default);
524
533 public Dictionary<K, V> ToDictionary<K, V>(Func<T, K> getKey, Func<T, V> getValue) where K : notnull;
543 public Task<Dictionary<K, V>> ToDictionaryAsync<K, V>(Func<T, K> getKey, Func<T, V> getValue, CancellationToken cancel = default) where K : notnull;
544
554 public Dictionary<K, V> ToDictionary<K, V>(Func<T, K> getKey, Func<T, V> getValue, IEqualityComparer<K>? comparer) where K : notnull;
565 public Task<Dictionary<K, V>> ToDictionaryAsync<K, V>(Func<T, K> getKey, Func<T, V> getValue, IEqualityComparer<K>? comparer, CancellationToken cancel = default) where K : notnull;
566}
Represents the options for customizing Fauna queries.
An interface for common static IQuerySource methods that are non-generic.
bool All(Expression< Func< T, bool > > predicate)
Applies each predicate and executes the query. This is evaluated server-side.
int Sum(Expression< Func< T, int > > selector)
Calculates the sum of values returned by a provided selector. This is evaluated server-side.
Task< T > LastAsync(CancellationToken cancel=default)
Executes the query asynchronously and obtains the last element in the result. This is evaluated serve...
T Min()
Executes a query and returns the minimum value in the result set. This is evaluated server-side....
Task< long > LongCountAsync(Expression< Func< T, bool > > predicate, CancellationToken cancel=default)
Applies the predicate, applies a count, and executes the query asynchronously. This is evaluated serv...
long LongCount(Expression< Func< T, bool > > predicate)
Applies the predicate, applies a count, and executes the query. This is evaluated server-side.
Task< T[]> ToArrayAsync(CancellationToken cancel=default)
Executes the query asynchronously and converts the results to a T:T[].
Task< T?> FirstOrDefaultAsync(CancellationToken cancel=default)
Executes the query asynchronously and obtains the first element in the result or the default value,...
IAsyncEnumerable< Page< T > > PaginateAsync(QueryOptions? queryOptions=null, CancellationToken cancel=default)
Executes a paginating query asynchronously and returns an enumerable of pages.
IQuerySource< T > Where(Expression< Func< T, bool > > predicate)
Applies the predicate to the query. This is evaluated server-side.
R Max< R >(Expression< Func< T, R > > selector)
Applies a selector, executes a query, and returns the maximum value in the result set....
Task< T > LastAsync(Expression< Func< T, bool > > predicate, CancellationToken cancel=default)
Applies the predicate, executes the query asynchronously, and obtains the last element in the result....
double Average(Expression< Func< T, double > > selector)
Calculates the mean average of values returned by a provided selector. This is evaluated server-side.
HashSet< T > ToHashSet(IEqualityComparer< T >? comparer)
Executes the query and converts the results to a HashSet<T>.
bool Any()
Determines if the result is not empty. This is evaluated server-side.
T? LastOrDefault()
Executes the query and obtains the last element in the result or the default value,...
IQuerySource< T > Distinct()
Obtains a distinct set of results. This is evaluated server-side.
Task< T > FirstAsync(CancellationToken cancel=default)
Executes the query asynchronously and obtains the first element in the result. This is evaluated serv...
IAsyncEnumerable< T > ToAsyncEnumerable(CancellationToken cancel=default)
Executes a query asynchronously.
IQuerySource< T > OrderByDescending< K >(Expression< Func< T, K > > keySelector)
Orders by descending according to the selector. This is evaluated server-side.
long Sum(Expression< Func< T, long > > selector)
Calculates the sum of values returned by a provided selector. This is evaluated server-side.
T? FirstOrDefault(Expression< Func< T, bool > > predicate)
Applies the predicate, executes the query, and obtains the first element in the result or the default...
Task< bool > AnyAsync(Expression< Func< T, bool > > predicate, CancellationToken cancel=default)
Applies each predicate and executes the query asynchronously. This is evaluated server-side.
Task< int > CountAsync(Expression< Func< T, bool > > predicate, CancellationToken cancel=default)
Applies the predicate, applies a count, and executes the query asynchronously. This is evaluated serv...
int Count(Expression< Func< T, bool > > predicate)
Applies the predicate, applies a count, and executes the query. This is evaluated server-side.
T[] ToArray()
Executes the query and converts the results to a T:T[].
Task< T > FirstAsync(Expression< Func< T, bool > > predicate, CancellationToken cancel=default)
Applies the predicate, executes the query asynchronously, and obtains the first element in the result...
IQuerySource< T > Take(int count)
Takes the first N elements of the results. This is evaluated server-side.
T Last(Expression< Func< T, bool > > predicate)
Applies the predicate, executes the query, and obtains the last element in the result....
int Count()
Applies a count of the elements and executes the query. This is evaluated server-side.
Task< T > SingleAsync(Expression< Func< T, bool > > predicate, CancellationToken cancel=default)
Applies the predicate and executes the query asynchronously. If the result is a single element,...
long LongCount()
Applies a count of the elements and executes the query. This is evaluated server-side.
Task< R > MinAsync< R >(Expression< Func< T, R > > selector, CancellationToken cancel=default)
Applies the selector, executes the query asynchronously, and returns the minimum value in the result ...
IQuerySource< T > Reverse()
Reverses the order of the results. This is evaluated server-side.
T Single(Expression< Func< T, bool > > predicate)
Applies the predicate and executes the query. If the result is a single element, returns it....
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>.
Task< R > MaxAsync< R >(Expression< Func< T, R > > selector, CancellationToken cancel=default)
Applies a selector, executes a query asynchronously, and returns the maximum value in the result set....
Task< double > AverageAsync(Expression< Func< T, double > > selector, CancellationToken cancel=default)
Asynchronously calculates the mean average of values returned by a provided selector....
Task< int > SumAsync(Expression< Func< T, int > > selector, CancellationToken cancel=default)
Asynchronously calculates the sum of values returned by a provided selector. This is evaluated server...
T First(Expression< Func< T, bool > > predicate)
Applies the predicate, executes the query, and obtains the first element in the result....
T Max()
Executes a query and returns the maximum value in the result set. This is evaluated server-side....
Task< T > MaxAsync(CancellationToken cancel=default)
Executes a query asynchronously and returns the maximum value in the result set. This is evaluated se...
IQuerySource< T > Skip(int count)
Skips the first N elements of the results. This is evaluated server-side.
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< T > SingleOrDefaultAsync(CancellationToken cancel=default)
Executes the query asynchronously. If the result is a single element, returns it. Otherwise,...
Task< HashSet< T > > ToHashSetAsync(CancellationToken cancel=default)
Executes the query asynchronously and converts the results to a HashSet<T>.
Task< T?> LastOrDefaultAsync(CancellationToken cancel=default)
Executes the query asynchronously and obtains the last element in the result or the default value,...
Task< long > SumAsync(Expression< Func< T, long > > selector, CancellationToken cancel=default)
Asynchronously calculates the sum of values returned by a provided selector. This is evaluated server...
IQuerySource< R > Select< R >(Expression< Func< T, R > > selector)
Applies a projection to the query. This is evaluated server-side.
HashSet< T > ToHashSet()
Executes the query and converts the results to a HashSet<T>.
IQuerySource< T > OrderDescending()
Orders by descending. This is evaluated server-side.
Task< List< T > > ToListAsync(CancellationToken cancel=default)
Executes the query asynchronously and converts the results to a List<T>.
Task< bool > AllAsync(Expression< Func< T, bool > > predicate, CancellationToken cancel=default)
Applies each predicate and executes the query asynchronously. This is evaluated server-side.
double Sum(Expression< Func< T, double > > selector)
Calculates the sum of values returned by a provided selector. This is evaluated server-side.
IQuerySource< T > OrderBy< K >(Expression< Func< T, K > > keySelector)
Orders according to the selector. This is evaluated server-side.
Task< T > SingleAsync(CancellationToken cancel=default)
Executes the query asynchronously. If the result is a single element, returns it. Otherwise,...
Task< long > LongCountAsync(CancellationToken cancel=default)
Applies a count of the elements and executes the query asynchronously. This is evaluated server-side.
Task< T > MinAsync(CancellationToken cancel=default)
Executes a query asynchronously and returns the minimum value in the result set. This is evaluated se...
Task< T?> FirstOrDefaultAsync(Expression< Func< T, bool > > predicate, CancellationToken cancel=default)
Applies the predicate, executes the query asynchronously, and obtains the first element in the result...
R Min< R >(Expression< Func< T, R > > selector)
Applies the selector, executes the query, and returns the minimum value in the result set....
T First()
Executes the query and obtains the first element in the result. This is evaluated server-side.
Task< T > SingleOrDefaultAsync(Expression< Func< T, bool > > predicate, CancellationToken cancel=default)
Applies the predicate and executes the query asynchronously. If the result is a single element,...
List< T > ToList()
Executes the query and converts the results to a List<T>.
T SingleOrDefault(Expression< Func< T, bool > > predicate)
Applies the predicate and executes the query. If the result is a single element, returns it....
IQuerySource< T > Order()
Applies default ordering to the query. This is evaluated server-side.
T Last()
Executes the query and obtains the last element in the result. This is evaluated server-side.
T SingleOrDefault()
Executes the query. If the result is a single element, returns it. Otherwise, returns the default....
IEnumerable< T > ToEnumerable()
Executes a query.
Task< int > CountAsync(CancellationToken cancel=default)
Applies a count of the elements and executes the query asynchronously. This is evaluated server-side.
bool Any(Expression< Func< T, bool > > predicate)
Applies each predicate and executes the query. This is evaluated server-side.
T Single()
Executes the query. If the result is a single element, returns it. Otherwise, throws an exception....
T? LastOrDefault(Expression< Func< T, bool > > predicate)
Applies the predicate, executes the query and obtains the last element in the result or the default v...
Task< HashSet< T > > ToHashSetAsync(IEqualityComparer< T >? comparer, CancellationToken cancel=default)
Executes the query asynchronously and converts the results to a HashSet<T>.
T? FirstOrDefault()
Executes the query and obtains the first element in the result or the default value,...
Task< T?> LastOrDefaultAsync(Expression< Func< T, bool > > predicate, CancellationToken cancel=default)
Applies the predicate, executes the query asynchronously and obtains the last element in the result o...
Task< double > SumAsync(Expression< Func< T, double > > selector, CancellationToken cancel=default)
Asynchronously calculates the sum of values returned by a provided selector. This is evaluated server...
Task< bool > AnyAsync(CancellationToken cancel=default)
Determines if the result is not empty asynchronously. This is evaluated server-side.