@@ -96,6 +96,20 @@ public static bool Any([NotNull] this IQueryable source, [NotNull] string predic
96
96
97
97
return Execute < bool > ( _anyPredicate , source , lambda ) ;
98
98
}
99
+
100
+ /// <summary>
101
+ /// Determines whether a sequence contains any elements.
102
+ /// </summary>
103
+ /// <param name="source">A sequence to check for being empty.</param>
104
+ /// <param name="lambda">A cached Lambda Expression.</param>
105
+ /// <returns>true if the source sequence contains any elements; otherwise, false.</returns>
106
+ public static bool Any ( [ NotNull ] this IQueryable source , [ NotNull ] LambdaExpression lambda )
107
+ {
108
+ Check . NotNull ( source , nameof ( source ) ) ;
109
+ Check . NotNull ( lambda , nameof ( lambda ) ) ;
110
+
111
+ return Execute < bool > ( _anyPredicate , source , lambda ) ;
112
+ }
99
113
#endregion Any
100
114
101
115
#region AsEnumerable
@@ -170,6 +184,20 @@ public static int Count([NotNull] this IQueryable source, [NotNull] string predi
170
184
171
185
return Execute < int > ( _countPredicate , source , lambda ) ;
172
186
}
187
+
188
+ /// <summary>
189
+ /// Returns the number of elements in a sequence.
190
+ /// </summary>
191
+ /// <param name="source">The <see cref="IQueryable"/> that contains the elements to be counted.</param>
192
+ /// <param name="lambda">A cached Lambda Expression.</param>
193
+ /// <returns>The number of elements in the specified sequence that satisfies a condition.</returns>
194
+ public static int Count ( [ NotNull ] this IQueryable source , [ NotNull ] LambdaExpression lambda )
195
+ {
196
+ Check . NotNull ( source , nameof ( source ) ) ;
197
+ Check . NotNull ( lambda , nameof ( lambda ) ) ;
198
+
199
+ return Execute < int > ( _countPredicate , source , lambda ) ;
200
+ }
173
201
#endregion Count
174
202
175
203
#region Distinct
@@ -238,6 +266,22 @@ public static dynamic First([NotNull] this IQueryable source, [NotNull] string p
238
266
239
267
return Execute ( _firstPredicate , source , lambda ) ;
240
268
}
269
+
270
+ /// <summary>
271
+ /// Returns the first element of a sequence that satisfies a specified condition.
272
+ /// </summary>
273
+ /// <param name="source">The <see cref="IQueryable"/> to return the first element of.</param>
274
+ /// <param name="lambda">A cached Lambda Expression.</param>
275
+ /// <returns>The first element in source that passes the test in predicate.</returns>
276
+ #if NET35
277
+ public static object First ( [ NotNull ] this IQueryable source , [ NotNull ] LambdaExpression lambda )
278
+ #else
279
+ public static dynamic First ( [ NotNull ] this IQueryable source , [ NotNull ] LambdaExpression lambda )
280
+ #endif
281
+ {
282
+ Check . NotNull ( source , nameof ( source ) ) ;
283
+ return Execute ( _firstPredicate , source , lambda ) ;
284
+ }
241
285
#endregion First
242
286
243
287
#region FirstOrDefault
@@ -279,6 +323,23 @@ public static dynamic FirstOrDefault([NotNull] this IQueryable source, [NotNull]
279
323
280
324
return Execute ( _firstOrDefaultPredicate , source , lambda ) ;
281
325
}
326
+
327
+ /// <summary>
328
+ /// Returns the first element of a sequence that satisfies a specified condition or a default value if no such element is found.
329
+ /// </summary>
330
+ /// <param name="source">The <see cref="IQueryable"/> to return the first element of.</param>
331
+ /// <param name="lambda">A cached Lambda Expression.</param>
332
+ /// <returns>default if source is empty or if no element passes the test specified by predicate; otherwise, the first element in source that passes the test specified by predicate.</returns>
333
+ #if NET35
334
+ public static object FirstOrDefault ( [ NotNull ] this IQueryable source , [ NotNull ] LambdaExpression lambda )
335
+ #else
336
+ public static dynamic FirstOrDefault ( [ NotNull ] this IQueryable source , [ NotNull ] LambdaExpression lambda )
337
+ #endif
338
+ {
339
+ Check . NotNull ( source , nameof ( source ) ) ;
340
+
341
+ return Execute ( _firstOrDefaultPredicate , source , lambda ) ;
342
+ }
282
343
private static readonly MethodInfo _firstOrDefaultPredicate = GetMethod ( nameof ( Queryable . FirstOrDefault ) , 1 ) ;
283
344
#endregion FirstOrDefault
284
345
@@ -563,6 +624,46 @@ public static dynamic Last([NotNull] this IQueryable source)
563
624
564
625
return Execute ( _last , source ) ;
565
626
}
627
+
628
+ private static readonly MethodInfo _lastPredicate = GetMethod ( nameof ( Queryable . Last ) , 1 ) ;
629
+
630
+ /// <summary>
631
+ /// Returns the last element of a sequence that satisfies a specified condition.
632
+ /// </summary>
633
+ /// <param name="source">The <see cref="IQueryable"/> to return the last element of.</param>
634
+ /// <param name="predicate">A function to test each element for a condition.</param>
635
+ /// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
636
+ /// <returns>The first element in source that passes the test in predicate.</returns>
637
+ #if NET35
638
+ public static object Last ( [ NotNull ] this IQueryable source , [ NotNull ] string predicate , params object [ ] args )
639
+ #else
640
+ public static dynamic Last ( [ NotNull ] this IQueryable source , [ NotNull ] string predicate , params object [ ] args )
641
+ #endif
642
+ {
643
+ Check . NotNull ( source , nameof ( source ) ) ;
644
+ Check . NotEmpty ( predicate , nameof ( predicate ) ) ;
645
+
646
+ bool createParameterCtor = source . IsLinqToObjects ( ) ;
647
+ LambdaExpression lambda = DynamicExpressionParser . ParseLambda ( createParameterCtor , source . ElementType , null , predicate , args ) ;
648
+
649
+ return Execute ( _lastPredicate , source , lambda ) ;
650
+ }
651
+
652
+ /// <summary>
653
+ /// Returns the last element of a sequence that satisfies a specified condition.
654
+ /// </summary>
655
+ /// <param name="source">The <see cref="IQueryable"/> to return the last element of.</param>
656
+ /// <param name="lambda">A cached Lambda Expression.</param>
657
+ /// <returns>The first element in source that passes the test in predicate.</returns>
658
+ #if NET35
659
+ public static object Last ( [ NotNull ] this IQueryable source , [ NotNull ] LambdaExpression lambda )
660
+ #else
661
+ public static dynamic Last ( [ NotNull ] this IQueryable source , [ NotNull ] LambdaExpression lambda )
662
+ #endif
663
+ {
664
+ Check . NotNull ( source , nameof ( source ) ) ;
665
+ return Execute ( _lastPredicate , source , lambda ) ;
666
+ }
566
667
#endregion Last
567
668
568
669
#region LastOrDefault
@@ -582,6 +683,46 @@ public static dynamic LastOrDefault([NotNull] this IQueryable source)
582
683
583
684
return Execute ( _lastDefault , source ) ;
584
685
}
686
+
687
+ private static readonly MethodInfo _lastDefaultPredicate = GetMethod ( nameof ( Queryable . LastOrDefault ) , 1 ) ;
688
+
689
+ /// <summary>
690
+ /// Returns the last element of a sequence that satisfies a specified condition, or a default value if the sequence contains no elements.
691
+ /// </summary>
692
+ /// <param name="source">The <see cref="IQueryable"/> to return the last element of.</param>
693
+ /// <param name="predicate">A function to test each element for a condition.</param>
694
+ /// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
695
+ /// <returns>The first element in source that passes the test in predicate.</returns>
696
+ #if NET35
697
+ public static object LastOrDefault ( [ NotNull ] this IQueryable source , [ NotNull ] string predicate , params object [ ] args )
698
+ #else
699
+ public static dynamic LastOrDefault ( [ NotNull ] this IQueryable source , [ NotNull ] string predicate , params object [ ] args )
700
+ #endif
701
+ {
702
+ Check . NotNull ( source , nameof ( source ) ) ;
703
+ Check . NotEmpty ( predicate , nameof ( predicate ) ) ;
704
+
705
+ bool createParameterCtor = source . IsLinqToObjects ( ) ;
706
+ LambdaExpression lambda = DynamicExpressionParser . ParseLambda ( createParameterCtor , source . ElementType , null , predicate , args ) ;
707
+
708
+ return Execute ( _lastDefaultPredicate , source , lambda ) ;
709
+ }
710
+
711
+ /// <summary>
712
+ /// Returns the last element of a sequence that satisfies a specified condition, or a default value if the sequence contains no elements.
713
+ /// </summary>
714
+ /// <param name="source">The <see cref="IQueryable"/> to return the last element of.</param>
715
+ /// <param name="lambda">A cached Lambda Expression.</param>
716
+ /// <returns>The first element in source that passes the test in predicate.</returns>
717
+ #if NET35
718
+ public static object LastOrDefault ( [ NotNull ] this IQueryable source , [ NotNull ] LambdaExpression lambda )
719
+ #else
720
+ public static dynamic LastOrDefault ( [ NotNull ] this IQueryable source , [ NotNull ] LambdaExpression lambda )
721
+ #endif
722
+ {
723
+ Check . NotNull ( source , nameof ( source ) ) ;
724
+ return Execute ( _lastDefaultPredicate , source , lambda ) ;
725
+ }
585
726
#endregion LastOrDefault
586
727
587
728
#region OrderBy
@@ -1066,6 +1207,48 @@ public static dynamic Single([NotNull] this IQueryable source)
1066
1207
return source . Provider . Execute ( optimized ) ;
1067
1208
}
1068
1209
1210
+ private static readonly MethodInfo _singlePredicate = GetMethod ( nameof ( Queryable . Single ) , 1 ) ;
1211
+
1212
+ /// <summary>
1213
+ /// Returns the only element of a sequence that satisfies a specified condition, and throws an exception if there
1214
+ /// is not exactly one element in the sequence.
1215
+ /// </summary>
1216
+ /// <param name="source">The <see cref="IQueryable"/> to return the last element of.</param>
1217
+ /// <param name="predicate">A function to test each element for a condition.</param>
1218
+ /// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
1219
+ /// <returns>The first element in source that passes the test in predicate.</returns>
1220
+ #if NET35
1221
+ public static object Single ( [ NotNull ] this IQueryable source , [ NotNull ] string predicate , params object [ ] args )
1222
+ #else
1223
+ public static dynamic Single ( [ NotNull ] this IQueryable source , [ NotNull ] string predicate , params object [ ] args )
1224
+ #endif
1225
+ {
1226
+ Check . NotNull ( source , nameof ( source ) ) ;
1227
+ Check . NotEmpty ( predicate , nameof ( predicate ) ) ;
1228
+
1229
+ bool createParameterCtor = source . IsLinqToObjects ( ) ;
1230
+ LambdaExpression lambda = DynamicExpressionParser . ParseLambda ( createParameterCtor , source . ElementType , null , predicate , args ) ;
1231
+
1232
+ return Execute ( _singlePredicate , source , lambda ) ;
1233
+ }
1234
+
1235
+ /// <summary>
1236
+ /// Returns the only element of a sequence that satisfies a specified condition, and throws an exception if there
1237
+ /// is not exactly one element in the sequence.
1238
+ /// </summary>
1239
+ /// <param name="source">The <see cref="IQueryable"/> to return the last element of.</param>
1240
+ /// <param name="lambda">A cached Lambda Expression.</param>
1241
+ /// <returns>The first element in source that passes the test in predicate.</returns>
1242
+ #if NET35
1243
+ public static object Single ( [ NotNull ] this IQueryable source , [ NotNull ] LambdaExpression lambda )
1244
+ #else
1245
+ public static dynamic Single ( [ NotNull ] this IQueryable source , [ NotNull ] LambdaExpression lambda )
1246
+ #endif
1247
+ {
1248
+ Check . NotNull ( source , nameof ( source ) ) ;
1249
+ return Execute ( _singlePredicate , source , lambda ) ;
1250
+ }
1251
+
1069
1252
/// <summary>
1070
1253
/// Returns the only element of a sequence, or a default value if the sequence
1071
1254
/// is empty; this method throws an exception if there is more than one element
@@ -1084,6 +1267,48 @@ public static dynamic SingleOrDefault([NotNull] this IQueryable source)
1084
1267
var optimized = OptimizeExpression ( Expression . Call ( typeof ( Queryable ) , "SingleOrDefault" , new [ ] { source . ElementType } , source . Expression ) ) ;
1085
1268
return source . Provider . Execute ( optimized ) ;
1086
1269
}
1270
+
1271
+ private static readonly MethodInfo _singleDefaultPredicate = GetMethod ( nameof ( Queryable . SingleOrDefault ) , 1 ) ;
1272
+
1273
+ /// <summary>
1274
+ /// Returns the only element of a sequence that satisfies a specified condition or a default value if the sequence
1275
+ /// is empty; and throws an exception if there is not exactly one element in the sequence.
1276
+ /// </summary>
1277
+ /// <param name="source">The <see cref="IQueryable"/> to return the last element of.</param>
1278
+ /// <param name="predicate">A function to test each element for a condition.</param>
1279
+ /// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
1280
+ /// <returns>The first element in source that passes the test in predicate.</returns>
1281
+ #if NET35
1282
+ public static object SingleOrDefault ( [ NotNull ] this IQueryable source , [ NotNull ] string predicate , params object [ ] args )
1283
+ #else
1284
+ public static dynamic SingleOrDefault ( [ NotNull ] this IQueryable source , [ NotNull ] string predicate , params object [ ] args )
1285
+ #endif
1286
+ {
1287
+ Check . NotNull ( source , nameof ( source ) ) ;
1288
+ Check . NotEmpty ( predicate , nameof ( predicate ) ) ;
1289
+
1290
+ bool createParameterCtor = source . IsLinqToObjects ( ) ;
1291
+ LambdaExpression lambda = DynamicExpressionParser . ParseLambda ( createParameterCtor , source . ElementType , null , predicate , args ) ;
1292
+
1293
+ return Execute ( _singleDefaultPredicate , source , lambda ) ;
1294
+ }
1295
+
1296
+ /// <summary>
1297
+ /// Returns the only element of a sequence that satisfies a specified condition or a default value if the sequence
1298
+ /// is empty; and throws an exception if there is not exactly one element in the sequence.
1299
+ /// </summary>
1300
+ /// <param name="source">The <see cref="IQueryable"/> to return the last element of.</param>
1301
+ /// <param name="lambda">A cached Lambda Expression.</param>
1302
+ /// <returns>The first element in source that passes the test in predicate.</returns>
1303
+ #if NET35
1304
+ public static object SingleOrDefault ( [ NotNull ] this IQueryable source , [ NotNull ] LambdaExpression lambda )
1305
+ #else
1306
+ public static dynamic SingleOrDefault ( [ NotNull ] this IQueryable source , [ NotNull ] LambdaExpression lambda )
1307
+ #endif
1308
+ {
1309
+ Check . NotNull ( source , nameof ( source ) ) ;
1310
+ return Execute ( _singleDefaultPredicate , source , lambda ) ;
1311
+ }
1087
1312
#endregion Single/SingleOrDefault
1088
1313
1089
1314
#region Skip
@@ -1314,6 +1539,20 @@ public static IQueryable Where([NotNull] this IQueryable source, [NotNull] strin
1314
1539
var optimized = OptimizeExpression ( Expression . Call ( typeof ( Queryable ) , "Where" , new [ ] { source . ElementType } , source . Expression , Expression . Quote ( lambda ) ) ) ;
1315
1540
return source . Provider . CreateQuery ( optimized ) ;
1316
1541
}
1542
+
1543
+ /// <summary>
1544
+ /// Filters a sequence of values based on a predicate.
1545
+ /// </summary>
1546
+ /// <param name="source">A <see cref="IQueryable"/> to filter.</param>
1547
+ /// <param name="lambda">A cached Lambda Expression.</param>
1548
+ public static IQueryable Where ( [ NotNull ] this IQueryable source , [ NotNull ] LambdaExpression lambda )
1549
+ {
1550
+ Check . NotNull ( source , nameof ( source ) ) ;
1551
+ Check . NotNull ( lambda , nameof ( lambda ) ) ;
1552
+
1553
+ var optimized = OptimizeExpression ( Expression . Call ( typeof ( Queryable ) , "Where" , new [ ] { source . ElementType } , source . Expression , Expression . Quote ( lambda ) ) ) ;
1554
+ return source . Provider . CreateQuery ( optimized ) ;
1555
+ }
1317
1556
#endregion
1318
1557
1319
1558
#region Private Helpers
0 commit comments