-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
MongoQueryable.cs
3575 lines (3235 loc) · 193 KB
/
MongoQueryable.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2015-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.Search;
namespace MongoDB.Driver.Linq
{
/// <summary>
/// Extension for <see cref="IMongoQueryable" />.
/// </summary>
public static class MongoQueryable
{
/// <summary>
/// Determines whether a sequence contains any elements.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">A sequence to check for being empty.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// true if the source sequence contains any elements; otherwise, false.
/// </returns>
public static Task<bool> AnyAsync<TSource>(this IMongoQueryable<TSource> source, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
return source.Provider.ExecuteAsync<bool>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, bool>(Queryable.Any, source),
source.Expression),
cancellationToken);
}
/// <summary>
/// Determines whether any element of a sequence satisfies a condition.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">A sequence whose elements to test for a condition.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// true if any elements in the source sequence pass the test in the specified predicate; otherwise, false.
/// </returns>
public static Task<bool> AnyAsync<TSource>(this IMongoQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(predicate, nameof(predicate));
return source.Provider.ExecuteAsync<bool>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, Expression<Func<TSource, bool>>, bool>(Queryable.Any, source, predicate),
source.Expression,
Expression.Quote(predicate)),
cancellationToken);
}
/// <summary>
/// Appends an arbitrary stage to the LINQ pipeline.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <typeparam name="TResult">The type of the result values returned by the appended stage.</typeparam>
/// <param name="source">A sequence of values.</param>
/// <param name="stage">The stage to append.</param>
/// <param name="resultSerializer">The result serializer.</param>
/// <returns>The queryable with a new stage appended.</returns>
public static IMongoQueryable<TResult> AppendStage<TSource, TResult>(
this IMongoQueryable<TSource> source,
PipelineStageDefinition<TSource, TResult> stage,
IBsonSerializer<TResult> resultSerializer = null)
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(stage, nameof(stage));
return (IMongoQueryable<TResult>)source.Provider.CreateQuery<TResult>(
Expression.Call(
null,
GetMethodInfo(AppendStage, source, stage, resultSerializer),
Expression.Convert(source.Expression, typeof(IMongoQueryable<TSource>)),
Expression.Constant(stage),
Expression.Constant(resultSerializer, typeof(IBsonSerializer<TResult>))));
}
/// <summary>
/// Allows the results to be interpreted as a different type. It is up to the caller
/// to determine that the new result type is compatible with the actual results.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <typeparam name="TResult">The new result type for the results.</typeparam>
/// <param name="source">A sequence of values.</param>
/// <param name="resultSerializer">The new serializer (optional, will be looked up if null).</param>
/// <returns>
/// A new IMongoQueryable with a new result type.
/// </returns>
public static IMongoQueryable<TResult> As<TSource, TResult>(
this IMongoQueryable<TSource> source,
IBsonSerializer<TResult> resultSerializer = null)
{
Ensure.IsNotNull(source, nameof(source));
return (IMongoQueryable<TResult>)source.Provider.CreateQuery<TResult>(
Expression.Call(
null,
GetMethodInfo(As, source, resultSerializer),
Expression.Convert(source.Expression, typeof(IMongoQueryable<TSource>)),
Expression.Constant(resultSerializer, typeof(IBsonSerializer<TResult>))));
}
/// <summary>
/// Computes the average of a sequence of <see cref="Decimal"/> values.
/// </summary>
/// <param name="source">A sequence of values to calculate the average of.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The average of the values in the sequence.</returns>
public static Task<decimal> AverageAsync(this IMongoQueryable<decimal> source, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
return source.Provider.ExecuteAsync<decimal>(
Expression.Call(
GetMethodInfo<IQueryable<decimal>, decimal>(Queryable.Average, source),
source.Expression),
cancellationToken);
}
/// <summary>
/// Computes the average of a sequence of <see cref="Nullable{Decimal}"/> values.
/// </summary>
/// <param name="source">A sequence of values to calculate the average of.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The average of the values in the sequence.</returns>
public static Task<decimal?> AverageAsync(this IMongoQueryable<decimal?> source, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
return source.Provider.ExecuteAsync<decimal?>(
Expression.Call(
GetMethodInfo<IQueryable<decimal?>, decimal?>(Queryable.Average, source),
source.Expression),
cancellationToken);
}
/// <summary>
/// Computes the average of a sequence of <see cref="Double"/> values.
/// </summary>
/// <param name="source">A sequence of values to calculate the average of.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The average of the values in the sequence.</returns>
public static Task<double> AverageAsync(this IMongoQueryable<double> source, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
return source.Provider.ExecuteAsync<double>(
Expression.Call(
GetMethodInfo<IQueryable<double>, double>(Queryable.Average, source),
source.Expression),
cancellationToken);
}
/// <summary>
/// Computes the average of a sequence of <see cref="Nullable{Double}"/> values.
/// </summary>
/// <param name="source">A sequence of values to calculate the average of.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The average of the values in the sequence.</returns>
public static Task<double?> AverageAsync(this IMongoQueryable<double?> source, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
return source.Provider.ExecuteAsync<double?>(
Expression.Call(
GetMethodInfo<IQueryable<double?>, double?>(Queryable.Average, source),
source.Expression),
cancellationToken);
}
/// <summary>
/// Computes the average of a sequence of <see cref="Single"/> values.
/// </summary>
/// <param name="source">A sequence of values to calculate the average of.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The average of the values in the sequence.</returns>
public static Task<float> AverageAsync(this IMongoQueryable<float> source, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
return source.Provider.ExecuteAsync<float>(
Expression.Call(
GetMethodInfo<IQueryable<float>, float>(Queryable.Average, source),
source.Expression),
cancellationToken);
}
/// <summary>
/// Computes the average of a sequence of <see cref="Nullable{Single}"/> values.
/// </summary>
/// <param name="source">A sequence of values to calculate the average of.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The average of the values in the sequence.</returns>
public static Task<float?> AverageAsync(this IMongoQueryable<float?> source, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
return source.Provider.ExecuteAsync<float?>(
Expression.Call(
GetMethodInfo<IQueryable<float?>, float?>(Queryable.Average, source),
source.Expression),
cancellationToken);
}
/// <summary>
/// Computes the average of a sequence of <see cref="Int32"/> values.
/// </summary>
/// <param name="source">A sequence of values to calculate the average of.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The average of the values in the sequence.</returns>
public static Task<double> AverageAsync(this IMongoQueryable<int> source, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
return source.Provider.ExecuteAsync<double>(
Expression.Call(
GetMethodInfo<IQueryable<int>, double>(Queryable.Average, source),
source.Expression),
cancellationToken);
}
/// <summary>
/// Computes the average of a sequence of <see cref="Nullable{Int32}"/> values.
/// </summary>
/// <param name="source">A sequence of values to calculate the average of.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The average of the values in the sequence.</returns>
public static Task<double?> AverageAsync(this IMongoQueryable<int?> source, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
return source.Provider.ExecuteAsync<double?>(
Expression.Call(
GetMethodInfo<IQueryable<int?>, double?>(Queryable.Average, source),
source.Expression),
cancellationToken);
}
/// <summary>
/// Computes the average of a sequence of <see cref="Int64"/> values.
/// </summary>
/// <param name="source">A sequence of values to calculate the average of.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The average of the values in the sequence.</returns>
public static Task<double> AverageAsync(this IMongoQueryable<long> source, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
return source.Provider.ExecuteAsync<double>(
Expression.Call(
GetMethodInfo<IQueryable<long>, double>(Queryable.Average, source),
source.Expression),
cancellationToken);
}
/// <summary>
/// Computes the average of a sequence of <see cref="Nullable{Int64}"/> values.
/// </summary>
/// <param name="source">A sequence of values to calculate the average of.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The average of the values in the sequence.</returns>
public static Task<double?> AverageAsync(this IMongoQueryable<long?> source, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
return source.Provider.ExecuteAsync<double?>(
Expression.Call(
GetMethodInfo<IQueryable<long?>, double?>(Queryable.Average, source),
source.Expression),
cancellationToken);
}
/// <summary>
/// Computes the average of the sequence of <see cref="Decimal" /> values that is obtained
/// by invoking a projection function on each element of the input sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">A sequence of values.</param>
/// <param name="selector">A projection function to apply to each element.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The average of the projected values.
/// </returns>
public static Task<decimal> AverageAsync<TSource>(this IMongoQueryable<TSource> source, Expression<Func<TSource, decimal>> selector, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(selector, nameof(selector));
return source.Provider.ExecuteAsync<decimal>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, Expression<Func<TSource, decimal>>, decimal>(Queryable.Average, source, selector),
source.Expression,
Expression.Quote(selector)),
cancellationToken);
}
/// <summary>
/// Computes the average of the sequence of <see cref="Nullable{Decimal}" /> values that is obtained
/// by invoking a projection function on each element of the input sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">A sequence of values.</param>
/// <param name="selector">A projection function to apply to each element.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The average of the projected values.
/// </returns>
public static Task<decimal?> AverageAsync<TSource>(this IMongoQueryable<TSource> source, Expression<Func<TSource, decimal?>> selector, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(selector, nameof(selector));
return source.Provider.ExecuteAsync<decimal?>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, Expression<Func<TSource, decimal?>>, decimal?>(Queryable.Average, source, selector),
source.Expression,
Expression.Quote(selector)),
cancellationToken);
}
/// <summary>
/// Computes the average of the sequence of <see cref="Double" /> values that is obtained
/// by invoking a projection function on each element of the input sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">A sequence of values.</param>
/// <param name="selector">A projection function to apply to each element.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The average of the projected values.
/// </returns>
public static Task<double> AverageAsync<TSource>(this IMongoQueryable<TSource> source, Expression<Func<TSource, double>> selector, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(selector, nameof(selector));
return source.Provider.ExecuteAsync<double>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, Expression<Func<TSource, double>>, double>(Queryable.Average, source, selector),
source.Expression,
Expression.Quote(selector)),
cancellationToken);
}
/// <summary>
/// Computes the average of the sequence of <see cref="Nullable{Double}" /> values that is obtained
/// by invoking a projection function on each element of the input sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">A sequence of values.</param>
/// <param name="selector">A projection function to apply to each element.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The average of the projected values.
/// </returns>
public static Task<double?> AverageAsync<TSource>(this IMongoQueryable<TSource> source, Expression<Func<TSource, double?>> selector, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(selector, nameof(selector));
return source.Provider.ExecuteAsync<double?>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, Expression<Func<TSource, double?>>, double?>(Queryable.Average, source, selector),
source.Expression,
Expression.Quote(selector)),
cancellationToken);
}
/// <summary>
/// Computes the average of the sequence of <see cref="Single" /> values that is obtained
/// by invoking a projection function on each element of the input sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">A sequence of values.</param>
/// <param name="selector">A projection function to apply to each element.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The average of the projected values.
/// </returns>
public static Task<float> AverageAsync<TSource>(this IMongoQueryable<TSource> source, Expression<Func<TSource, float>> selector, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(selector, nameof(selector));
return source.Provider.ExecuteAsync<float>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, Expression<Func<TSource, float>>, float>(Queryable.Average, source, selector),
source.Expression,
Expression.Quote(selector)),
cancellationToken);
}
/// <summary>
/// Computes the average of the sequence of <see cref="Nullable{Single}" /> values that is obtained
/// by invoking a projection function on each element of the input sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">A sequence of values.</param>
/// <param name="selector">A projection function to apply to each element.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The average of the projected values.
/// </returns>
public static Task<float?> AverageAsync<TSource>(this IMongoQueryable<TSource> source, Expression<Func<TSource, float?>> selector, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(selector, nameof(selector));
return source.Provider.ExecuteAsync<float?>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, Expression<Func<TSource, float?>>, float?>(Queryable.Average, source, selector),
source.Expression,
Expression.Quote(selector)),
cancellationToken);
}
/// <summary>
/// Computes the average of the sequence of <see cref="Int32" /> values that is obtained
/// by invoking a projection function on each element of the input sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">A sequence of values.</param>
/// <param name="selector">A projection function to apply to each element.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The average of the projected values.
/// </returns>
public static Task<double> AverageAsync<TSource>(this IMongoQueryable<TSource> source, Expression<Func<TSource, int>> selector, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(selector, nameof(selector));
return source.Provider.ExecuteAsync<double>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, Expression<Func<TSource, int>>, double>(Queryable.Average, source, selector),
source.Expression,
Expression.Quote(selector)),
cancellationToken);
}
/// <summary>
/// Computes the average of the sequence of <see cref="Nullable{Int32}" /> values that is obtained
/// by invoking a projection function on each element of the input sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">A sequence of values.</param>
/// <param name="selector">A projection function to apply to each element.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The average of the projected values.
/// </returns>
public static Task<double?> AverageAsync<TSource>(this IMongoQueryable<TSource> source, Expression<Func<TSource, int?>> selector, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(selector, nameof(selector));
return source.Provider.ExecuteAsync<double?>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, Expression<Func<TSource, int?>>, double?>(Queryable.Average, source, selector),
source.Expression,
Expression.Quote(selector)),
cancellationToken);
}
/// <summary>
/// Computes the average of the sequence of <see cref="Int64" /> values that is obtained
/// by invoking a projection function on each element of the input sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">A sequence of values.</param>
/// <param name="selector">A projection function to apply to each element.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The average of the projected values.
/// </returns>
public static Task<double> AverageAsync<TSource>(this IMongoQueryable<TSource> source, Expression<Func<TSource, long>> selector, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(selector, nameof(selector));
return source.Provider.ExecuteAsync<double>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, Expression<Func<TSource, long>>, double>(Queryable.Average, source, selector),
source.Expression,
Expression.Quote(selector)),
cancellationToken);
}
/// <summary>
/// Computes the average of the sequence of <see cref="Nullable{Int64}" /> values that is obtained
/// by invoking a projection function on each element of the input sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">A sequence of values.</param>
/// <param name="selector">A projection function to apply to each element.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The average of the projected values.
/// </returns>
public static Task<double?> AverageAsync<TSource>(this IMongoQueryable<TSource> source, Expression<Func<TSource, long?>> selector, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(selector, nameof(selector));
return source.Provider.ExecuteAsync<double?>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, Expression<Func<TSource, long?>>, double?>(Queryable.Average, source, selector),
source.Expression,
Expression.Quote(selector)),
cancellationToken);
}
/// <summary>
/// Returns the number of elements in a sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">The <see cref="IMongoQueryable{TSource}" /> that contains the elements to be counted.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The number of elements in the input sequence.
/// </returns>
public static Task<int> CountAsync<TSource>(this IMongoQueryable<TSource> source, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
return source.Provider.ExecuteAsync<int>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, int>(Queryable.Count, source),
source.Expression),
cancellationToken);
}
/// <summary>
/// Returns the number of elements in the specified sequence that satisfies a condition.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">An <see cref="IMongoQueryable{TSource}" /> that contains the elements to be counted.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The number of elements in the sequence that satisfies the condition in the predicate function.
/// </returns>
public static Task<int> CountAsync<TSource>(this IMongoQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(predicate, nameof(predicate));
return source.Provider.ExecuteAsync<int>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, Expression<Func<TSource, bool>>, int>(Queryable.Count, source, predicate),
source.Expression,
Expression.Quote(predicate)),
cancellationToken);
}
/// <summary>
/// Adds a $densify stage to the LINQ pipeline.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">A sequence of values.</param>
/// <param name="field">The field.</param>
/// <param name="range">The range.</param>
/// <param name="partitionByFields">The partition by fields.</param>
/// <returns>The densified sequence of values.</returns>
public static IMongoQueryable<TSource> Densify<TSource>(
this IMongoQueryable<TSource> source,
Expression<Func<TSource, object>> field,
DensifyRange range,
IEnumerable<Expression<Func<TSource, object>>> partitionByFields = null)
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(field, nameof(field));
Ensure.IsNotNull(range, nameof(range));
return Densify(source, field, range, partitionByFields?.ToArray());
}
/// <summary>
/// Adds a $densify stage to the LINQ pipeline.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">A sequence of values.</param>
/// <param name="field">The field.</param>
/// <param name="range">The range.</param>
/// <param name="partitionByFields">The partition by fields.</param>
/// <returns>The densified sequence of values.</returns>
public static IMongoQueryable<TSource> Densify<TSource>(
this IMongoQueryable<TSource> source,
Expression<Func<TSource, object>> field,
DensifyRange range,
params Expression<Func<TSource, object>>[] partitionByFields)
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(field, nameof(field));
Ensure.IsNotNull(range, nameof(range));
Expression quotedPartitionByFields;
if (partitionByFields?.Length > 0)
{
quotedPartitionByFields = Expression.NewArrayInit(typeof(Expression<Func<TSource, object>>), partitionByFields.Select(f => Expression.Quote(f)));
}
else
{
quotedPartitionByFields = Expression.Constant(null, typeof(Expression<Func<TSource, object>>[]));
}
return (IMongoQueryable<TSource>)source.Provider.CreateQuery<TSource>(
Expression.Call(
GetMethodInfo(MongoQueryable.Densify, source, field, range, partitionByFields),
Expression.Convert(source.Expression, typeof(IMongoQueryable<TSource>)),
Expression.Quote(field),
Expression.Constant(range),
quotedPartitionByFields));
}
/// <summary>
/// Returns distinct elements from a sequence by using the default equality comparer to compare values.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">The <see cref="IMongoQueryable{TSource}" /> to remove duplicates from.</param>
/// <returns>
/// An <see cref="IMongoQueryable{TSource}" /> that contains distinct elements from <paramref name="source" />.
/// </returns>
public static IMongoQueryable<TSource> Distinct<TSource>(this IMongoQueryable<TSource> source)
{
Ensure.IsNotNull(source, nameof(source));
return (IMongoQueryable<TSource>)Queryable.Distinct(source);
}
/// <summary>
/// Injects a sequence of documents at the beginning of a pipeline.
/// </summary>
/// <typeparam name="TDocument"> The type of the documents.</typeparam>
/// <param name="source">An IMongoQueryable with no other input.</param>
/// <param name="documents">The documents.</param>
/// <returns>
/// An <see cref="IMongoQueryable{TDocument}"/> whose elements are the documents.
/// </returns>
public static IMongoQueryable<TDocument> Documents<TDocument>(this IMongoQueryable<NoPipelineInput> source, params TDocument[] documents)
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(documents, nameof(documents));
return (IMongoQueryable<TDocument>)source.Provider.CreateQuery<TDocument>(
Expression.Call(
null,
GetMethodInfo(Documents, source, documents),
Expression.Convert(source.Expression, typeof(IMongoQueryable<NoPipelineInput>)),
Expression.Constant(documents, typeof(TDocument[]))));
}
/// <summary>
/// Injects a sequence of documents at the beginning of a pipeline.
/// </summary>
/// <typeparam name="TDocument"> The type of the documents.</typeparam>
/// <param name="source">An IMongoQueryable with no other input.</param>
/// <param name="documents">The documents.</param>
/// <param name="documentSerializer">The document serializer.</param>
/// <returns>
/// An <see cref="IMongoQueryable{TDocument}"/> whose elements are the documents.
/// </returns>
public static IMongoQueryable<TDocument> Documents<TDocument>(this IMongoQueryable<NoPipelineInput> source, IEnumerable<TDocument> documents, IBsonSerializer<TDocument> documentSerializer)
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(documents, nameof(documents));
Ensure.IsNotNull(documentSerializer, nameof(documentSerializer));
return (IMongoQueryable<TDocument>)source.Provider.CreateQuery<TDocument>(
Expression.Call(
null,
GetMethodInfo(Documents, source, documents, documentSerializer),
Expression.Convert(source.Expression, typeof(IMongoQueryable<NoPipelineInput>)),
Expression.Constant(documents, typeof(IEnumerable<TDocument>)),
Expression.Constant(documentSerializer, typeof(IBsonSerializer<TDocument>))));
}
/// <summary>
/// Returns the first element of a sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">The <see cref="IMongoQueryable{TSource}" /> to return the first element of.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The first element in <paramref name="source" />.
/// </returns>
public static Task<TSource> FirstAsync<TSource>(this IMongoQueryable<TSource> source, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
return source.Provider.ExecuteAsync<TSource>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, TSource>(Queryable.First, source),
source.Expression),
cancellationToken);
}
/// <summary>
/// Returns the first element of a sequence that satisfies a specified condition.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">An <see cref="IMongoQueryable{TSource}" /> to return an element from.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The first element in <paramref name="source" /> that passes the test in <paramref name="predicate" />.
/// </returns>
public static Task<TSource> FirstAsync<TSource>(this IMongoQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(predicate, nameof(predicate));
return source.Provider.ExecuteAsync<TSource>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, Expression<Func<TSource, bool>>, TSource>(Queryable.First, source, predicate),
source.Expression,
Expression.Quote(predicate)),
cancellationToken);
}
/// <summary>
/// Returns the first element of a sequence, or a default value if the sequence contains no elements.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">The <see cref="IMongoQueryable{TSource}" /> to return the first element of.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// default(<typeparamref name="TSource" />) if <paramref name="source" /> is empty; otherwise, the first element in <paramref name="source" />.
/// </returns>
public static Task<TSource> FirstOrDefaultAsync<TSource>(this IMongoQueryable<TSource> source, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
return source.Provider.ExecuteAsync<TSource>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, TSource>(Queryable.FirstOrDefault, source),
source.Expression),
cancellationToken);
}
/// <summary>
/// Returns the first element of a sequence that satisfies a specified condition or a default value if no such element is found.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">An <see cref="IMongoQueryable{TSource}" /> to return an element from.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// default(<typeparamref name="TSource" />) if <paramref name="source" /> is empty or if no element passes the test specified by <paramref name="predicate" />; otherwise, the first element in <paramref name="source" /> that passes the test specified by <paramref name="predicate" />.
/// </returns>
public static Task<TSource> FirstOrDefaultAsync<TSource>(this IMongoQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(predicate, nameof(predicate));
return source.Provider.ExecuteAsync<TSource>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, Expression<Func<TSource, bool>>, TSource>(Queryable.FirstOrDefault, source, predicate),
source.Expression,
Expression.Quote(predicate)),
cancellationToken);
}
/// <summary>
/// Groups the elements of a sequence according to a specified key selector function.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <typeparam name="TKey">The type of the key returned by the function represented in keySelector.</typeparam>
/// <param name="source">An <see cref="IMongoQueryable{TSource}" /> whose elements to group.</param>
/// <param name="keySelector">A function to extract the key for each element.</param>
/// <returns>
/// An <see cref="IMongoQueryable{T}" /> that has a type argument of <see cref="IGrouping{TKey, TSource}"/>
/// and where each <see cref="IGrouping{TKey, TSource}"/> object contains a sequence of objects
/// and a key.
/// </returns>
public static IMongoQueryable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IMongoQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(keySelector, nameof(keySelector));
return (IMongoQueryable<IGrouping<TKey, TSource>>)Queryable.GroupBy(source, keySelector);
}
/// <summary>
/// Groups the elements of a sequence according to a specified key selector function
/// and creates a result value from each group and its key.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <typeparam name="TKey">The type of the key returned by the function represented in keySelector.</typeparam>
/// <typeparam name="TResult">The type of the result value returned by resultSelector.</typeparam>
/// <param name="source">An <see cref="IMongoQueryable{TSource}" /> whose elements to group.</param>
/// <param name="keySelector">A function to extract the key for each element.</param>
/// <param name="resultSelector">A function to create a result value from each group.</param>
/// <returns>
/// An <see cref="IMongoQueryable{T}" /> that has a type argument of TResult and where
/// each element represents a projection over a group and its key.
/// </returns>
public static IMongoQueryable<TResult> GroupBy<TSource, TKey, TResult>(this IMongoQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, Expression<Func<TKey, IEnumerable<TSource>, TResult>> resultSelector)
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(keySelector, nameof(keySelector));
Ensure.IsNotNull(resultSelector, nameof(resultSelector));
return (IMongoQueryable<TResult>)Queryable.GroupBy(source, keySelector, resultSelector);
}
/// <summary>
/// Correlates the elements of two sequences based on key equality and groups the results.
/// </summary>
/// <typeparam name="TOuter">The type of the elements of the first sequence.</typeparam>
/// <typeparam name="TInner">The type of the elements of the second sequence.</typeparam>
/// <typeparam name="TKey">The type of the keys returned by the key selector functions.</typeparam>
/// <typeparam name="TResult">The type of the result elements.</typeparam>
/// <param name="outer">The first sequence to join.</param>
/// <param name="inner">The sequence to join to the first sequence.</param>
/// <param name="outerKeySelector">A function to extract the join key from each element of the first sequence.</param>
/// <param name="innerKeySelector">A function to extract the join key from each element of the second sequence.</param>
/// <param name="resultSelector">A function to create a result element from an element from the first sequence and a collection of matching elements from the second sequence.</param>
/// <returns>
/// An <see cref="IMongoQueryable{TResult}" /> that contains elements of type <typeparamref name="TResult" /> obtained by performing a grouped join on two sequences.
/// </returns>
public static IMongoQueryable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IMongoQueryable<TOuter> outer, IEnumerable<TInner> inner, Expression<Func<TOuter, TKey>> outerKeySelector, Expression<Func<TInner, TKey>> innerKeySelector, Expression<Func<TOuter, IEnumerable<TInner>, TResult>> resultSelector)
{
Ensure.IsNotNull(outer, nameof(outer));
Ensure.IsNotNull(inner, nameof(inner));
Ensure.IsNotNull(outerKeySelector, nameof(outerKeySelector));
Ensure.IsNotNull(innerKeySelector, nameof(innerKeySelector));
Ensure.IsNotNull(resultSelector, nameof(resultSelector));
return (IMongoQueryable<TResult>)Queryable.GroupJoin(outer, inner, outerKeySelector, innerKeySelector, resultSelector);
}
/// <summary>
/// Correlates the elements of two sequences based on key equality and groups the results.
/// </summary>
/// <typeparam name="TOuter">The type of the elements of the first sequence.</typeparam>
/// <typeparam name="TInner">The type of the elements of the second sequence.</typeparam>
/// <typeparam name="TKey">The type of the keys returned by the key selector functions.</typeparam>
/// <typeparam name="TResult">The type of the result elements.</typeparam>
/// <param name="outer">The first sequence to join.</param>
/// <param name="inner">The collection to join to the first sequence.</param>
/// <param name="outerKeySelector">A function to extract the join key from each element of the first sequence.</param>
/// <param name="innerKeySelector">A function to extract the join key from each element of the second sequence.</param>
/// <param name="resultSelector">A function to create a result element from an element from the first sequence and a collection of matching elements from the second sequence.</param>
/// <returns>
/// An <see cref="IMongoQueryable{TResult}" /> that contains elements of type <typeparamref name="TResult" /> obtained by performing a grouped join on two sequences.
/// </returns>
public static IMongoQueryable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IMongoQueryable<TOuter> outer, IMongoCollection<TInner> inner, Expression<Func<TOuter, TKey>> outerKeySelector, Expression<Func<TInner, TKey>> innerKeySelector, Expression<Func<TOuter, IEnumerable<TInner>, TResult>> resultSelector)
{
Ensure.IsNotNull(outer, nameof(outer));
Ensure.IsNotNull(inner, nameof(inner));
Ensure.IsNotNull(outerKeySelector, nameof(outerKeySelector));
Ensure.IsNotNull(innerKeySelector, nameof(innerKeySelector));
Ensure.IsNotNull(resultSelector, nameof(resultSelector));
return GroupJoin(outer, inner.AsQueryable(), outerKeySelector, innerKeySelector, resultSelector);
}
/// <summary>
/// Correlates the elements of two sequences based on matching keys.
/// </summary>
/// <typeparam name="TOuter">The type of the elements of the first sequence.</typeparam>
/// <typeparam name="TInner">The type of the elements of the second sequence.</typeparam>
/// <typeparam name="TKey">The type of the keys returned by the key selector functions.</typeparam>
/// <typeparam name="TResult">The type of the result elements.</typeparam>
/// <param name="outer">The first sequence to join.</param>
/// <param name="inner">The sequence to join to the first sequence.</param>
/// <param name="outerKeySelector">A function to extract the join key from each element of the first sequence.</param>
/// <param name="innerKeySelector">A function to extract the join key from each element of the second sequence.</param>
/// <param name="resultSelector">A function to create a result element from two matching elements.</param>
/// <returns>
/// An <see cref="T:System.Linq.IQueryable`1" /> that has elements of type <typeparamref name="TResult" /> obtained by performing an inner join on two sequences.
/// </returns>
public static IMongoQueryable<TResult> Join<TOuter, TInner, TKey, TResult>(this IMongoQueryable<TOuter> outer, IEnumerable<TInner> inner, Expression<Func<TOuter, TKey>> outerKeySelector, Expression<Func<TInner, TKey>> innerKeySelector, Expression<Func<TOuter, TInner, TResult>> resultSelector)
{
Ensure.IsNotNull(outer, nameof(outer));
Ensure.IsNotNull(inner, nameof(inner));
Ensure.IsNotNull(outerKeySelector, nameof(outerKeySelector));
Ensure.IsNotNull(innerKeySelector, nameof(innerKeySelector));
Ensure.IsNotNull(resultSelector, nameof(resultSelector));
return (IMongoQueryable<TResult>)Queryable.Join(outer, inner.AsQueryable(), outerKeySelector, innerKeySelector, resultSelector);
}
/// <summary>
/// Correlates the elements of two sequences based on matching keys.
/// </summary>
/// <typeparam name="TOuter">The type of the elements of the first sequence.</typeparam>
/// <typeparam name="TInner">The type of the elements of the second sequence.</typeparam>
/// <typeparam name="TKey">The type of the keys returned by the key selector functions.</typeparam>
/// <typeparam name="TResult">The type of the result elements.</typeparam>
/// <param name="outer">The first sequence to join.</param>
/// <param name="inner">The sequence to join to the first sequence.</param>
/// <param name="outerKeySelector">A function to extract the join key from each element of the first sequence.</param>
/// <param name="innerKeySelector">A function to extract the join key from each element of the second sequence.</param>
/// <param name="resultSelector">A function to create a result element from two matching elements.</param>
/// <returns>
/// An <see cref="T:System.Linq.IQueryable`1" /> that has elements of type <typeparamref name="TResult" /> obtained by performing an inner join on two sequences.
/// </returns>
public static IMongoQueryable<TResult> Join<TOuter, TInner, TKey, TResult>(this IMongoQueryable<TOuter> outer, IMongoCollection<TInner> inner, Expression<Func<TOuter, TKey>> outerKeySelector, Expression<Func<TInner, TKey>> innerKeySelector, Expression<Func<TOuter, TInner, TResult>> resultSelector)
{
Ensure.IsNotNull(outer, nameof(outer));
Ensure.IsNotNull(inner, nameof(inner));
Ensure.IsNotNull(outerKeySelector, nameof(outerKeySelector));
Ensure.IsNotNull(innerKeySelector, nameof(innerKeySelector));
Ensure.IsNotNull(resultSelector, nameof(resultSelector));
return Join(outer, inner.AsQueryable(), outerKeySelector, innerKeySelector, resultSelector);
}
/// <summary>
/// Returns the number of elements in a sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">The <see cref="IMongoQueryable{TSource}" /> that contains the elements to be counted.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The number of elements in the input sequence.
/// </returns>
public static Task<long> LongCountAsync<TSource>(this IMongoQueryable<TSource> source, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
return source.Provider.ExecuteAsync<long>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, long>(Queryable.LongCount, source),
source.Expression),
cancellationToken);
}
/// <summary>
/// Returns the number of elements in the specified sequence that satisfies a condition.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">An <see cref="IMongoQueryable{TSource}" /> that contains the elements to be counted.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The number of elements in the sequence that satisfies the condition in the predicate function.
/// </returns>
public static Task<long> LongCountAsync<TSource>(this IMongoQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
Ensure.IsNotNull(predicate, nameof(predicate));
return source.Provider.ExecuteAsync<long>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, Expression<Func<TSource, bool>>, long>(Queryable.LongCount, source, predicate),
source.Expression,
Expression.Quote(predicate)),
cancellationToken);
}
/// <summary>
/// Returns the maximum value in a generic <see cref="IMongoQueryable{TSource}" />.
/// </summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">A sequence of values to determine the maximum of.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The maximum value in the sequence.
/// </returns>
public static Task<TSource> MaxAsync<TSource>(this IMongoQueryable<TSource> source, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(source, nameof(source));
return source.Provider.ExecuteAsync<TSource>(
Expression.Call(
GetMethodInfo<IQueryable<TSource>, TSource>(Queryable.Max, source),
source.Expression),
cancellationToken);
}
/// <summary>
/// Invokes a projection function on each element of a generic <see cref="IMongoQueryable{TSource}" /> and returns the maximum resulting value.