@@ -28,6 +28,7 @@ namespace EntityFramework.DynamicLinq
28
28
/// </summary>
29
29
public static class EntityFrameworkDynamicQueryableExtensions
30
30
{
31
+ // ReSharper disable once UseNameOfInsteadOfTypeOf
31
32
private static readonly TraceSource TraceSource = new TraceSource ( typeof ( EntityFrameworkDynamicQueryableExtensions ) . Name ) ;
32
33
33
34
#region AllAsync
@@ -83,9 +84,9 @@ public static Task<bool> AllAsync([NotNull] this IQueryable source, [NotNull] st
83
84
84
85
return ExecuteAsync < bool > ( _AllPredicate , source , Expression . Quote ( lambda ) , cancellationToken ) ;
85
86
}
86
- #endregion AllAsync
87
+ #endregion AllAsync
87
88
88
- #region AnyAsync
89
+ #region AnyAsync
89
90
private static readonly MethodInfo _any = GetMethod ( nameof ( Queryable . Any ) ) ;
90
91
91
92
/// <summary>
@@ -165,10 +166,10 @@ public static Task<bool> AnyAsync([NotNull] this IQueryable source, [NotNull] st
165
166
166
167
return ExecuteAsync < bool > ( _anyPredicate , source , Expression . Quote ( lambda ) , cancellationToken ) ;
167
168
}
168
- #endregion AnyAsync
169
+ #endregion AnyAsync
169
170
170
- #region AverageAsync
171
- private static readonly MethodInfo _average = GetMethod ( nameof ( Queryable . Average ) ) ;
171
+ #region AverageAsync
172
+ private static readonly MethodInfo _averageForDouble = GetMethod ( nameof ( Queryable . Average ) , 0 , mi => mi . ReturnType == typeof ( double ) ) ;
172
173
173
174
/// <summary>
174
175
/// Asynchronously computes the average of a sequence of values.
@@ -192,10 +193,10 @@ public static Task<bool> AnyAsync([NotNull] this IQueryable source, [NotNull] st
192
193
Check . NotNull ( source , nameof ( source ) ) ;
193
194
Check . NotNull ( cancellationToken , nameof ( cancellationToken ) ) ;
194
195
195
- return ExecuteAsync < double > ( _average , source , cancellationToken ) ;
196
+ return ExecuteAsync < double > ( _averageForDouble , source , cancellationToken ) ;
196
197
}
197
198
198
- private static readonly MethodInfo _averagePredicate = GetMethod ( nameof ( Queryable . Average ) , 1 ) ;
199
+ private static readonly MethodInfo _averagePredicateForDouble = GetMethod ( nameof ( Queryable . Average ) , 1 , mi => mi . ReturnType == typeof ( double ) ) ;
199
200
200
201
/// <summary>
201
202
/// Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.
@@ -249,11 +250,11 @@ public static Task<double> AverageAsync([NotNull] this IQueryable source, [NotNu
249
250
250
251
LambdaExpression lambda = DynamicExpressionParser . ParseLambda ( false , source . ElementType , null , selector , args ) ;
251
252
252
- return ExecuteAsync < double > ( _averagePredicate , source , Expression . Quote ( lambda ) , cancellationToken ) ;
253
+ return ExecuteAsync < double > ( _averagePredicateForDouble , source , Expression . Quote ( lambda ) , cancellationToken ) ;
253
254
}
254
- #endregion AverageAsync
255
+ #endregion AverageAsync
255
256
256
- #region Count
257
+ #region Count
257
258
private static readonly MethodInfo _count = GetMethod ( nameof ( Queryable . Count ) ) ;
258
259
259
260
/// <summary>
@@ -338,9 +339,9 @@ public static Task<int> CountAsync([NotNull] this IQueryable source, Cancellatio
338
339
339
340
return ExecuteAsync < int > ( _countPredicate , source , Expression . Quote ( lambda ) , cancellationToken ) ;
340
341
}
341
- #endregion Count
342
+ #endregion Count
342
343
343
- #region FirstAsync
344
+ #region FirstAsync
344
345
private static readonly MethodInfo _first = GetMethod ( nameof ( Queryable . First ) ) ;
345
346
346
347
/// <summary>
@@ -423,9 +424,9 @@ public static Task<dynamic> FirstAsync([NotNull] this IQueryable source, Cancell
423
424
424
425
return ExecuteAsync < dynamic > ( _firstPredicate , source , Expression . Quote ( lambda ) , cancellationToken ) ;
425
426
}
426
- #endregion FirstAsync
427
+ #endregion FirstAsync
427
428
428
- #region FirstOrDefaultAsync
429
+ #region FirstOrDefaultAsync
429
430
private static readonly MethodInfo _firstOrDefault = GetMethod ( nameof ( Queryable . FirstOrDefault ) ) ;
430
431
431
432
/// <summary>
@@ -455,7 +456,7 @@ public static Task<dynamic> FirstAsync([NotNull] this IQueryable source, Cancell
455
456
return ExecuteAsync < dynamic > ( _firstOrDefault , source , cancellationToken ) ;
456
457
}
457
458
458
- private static readonly MethodInfo _firstOrDefaultPredicate = GetMethod ( nameof ( Queryable . FirstOrDefault ) , 1 ) ;
459
+ private static readonly MethodInfo _firstOrDefaultPredicate = GetMethod ( nameof ( Queryable . FirstOrDefault ) , 1 , mi => mi . GetParameters ( ) [ 1 ] . Name == "predicate" ) ;
459
460
460
461
/// <summary>
461
462
/// Asynchronously returns the first element of a sequence that satisfies a specified condition
@@ -515,9 +516,9 @@ public static Task<dynamic> FirstOrDefaultAsync([NotNull] this IQueryable source
515
516
516
517
return ExecuteAsync < dynamic > ( _firstOrDefaultPredicate , source , Expression . Quote ( lambda ) , cancellationToken ) ;
517
518
}
518
- #endregion FirstOrDefault
519
+ #endregion FirstOrDefault
519
520
520
- #region LastAsync
521
+ #region LastAsync
521
522
private static readonly MethodInfo _last = GetMethod ( nameof ( Queryable . Last ) ) ;
522
523
523
524
/// <summary>
@@ -600,9 +601,9 @@ public static Task<dynamic> LastAsync([NotNull] this IQueryable source, Cancella
600
601
601
602
return ExecuteAsync < dynamic > ( _lastPredicate , source , Expression . Quote ( lambda ) , cancellationToken ) ;
602
603
}
603
- #endregion LastAsync
604
+ #endregion LastAsync
604
605
605
- #region LastOrDefaultAsync
606
+ #region LastOrDefaultAsync
606
607
private static readonly MethodInfo _lastOrDefault = GetMethod ( nameof ( Queryable . LastOrDefault ) ) ;
607
608
608
609
/// <summary>
@@ -692,9 +693,9 @@ public static Task<dynamic> LastOrDefaultAsync([NotNull] this IQueryable source,
692
693
693
694
return ExecuteAsync < dynamic > ( _lastOrDefaultPredicate , source , Expression . Quote ( lambda ) , cancellationToken ) ;
694
695
}
695
- #endregion LastOrDefault
696
+ #endregion LastOrDefault
696
697
697
- #region LongCount
698
+ #region LongCount
698
699
private static readonly MethodInfo _longCount = GetMethod ( nameof ( Queryable . LongCount ) ) ;
699
700
700
701
/// <summary>
@@ -779,9 +780,9 @@ public static Task<long> LongCountAsync([NotNull] this IQueryable source, Cancel
779
780
780
781
return ExecuteAsync < long > ( _longCountPredicate , source , Expression . Quote ( lambda ) , cancellationToken ) ;
781
782
}
782
- #endregion LongCount
783
+ #endregion LongCount
783
784
784
- #region SingleOrDefaultAsync
785
+ #region SingleOrDefaultAsync
785
786
private static readonly MethodInfo _singleOrDefault = GetMethod ( nameof ( Queryable . SingleOrDefault ) ) ;
786
787
787
788
/// <summary>
@@ -810,7 +811,7 @@ public static Task<long> LongCountAsync([NotNull] this IQueryable source, Cancel
810
811
return ExecuteAsync < dynamic > ( _singleOrDefault , source , cancellationToken ) ;
811
812
}
812
813
813
- private static readonly MethodInfo _singleOrDefaultPredicate = GetMethod ( nameof ( Queryable . SingleOrDefault ) , 1 ) ;
814
+ private static readonly MethodInfo _singleOrDefaultPredicate = GetMethod ( nameof ( Queryable . SingleOrDefault ) , 1 , mi => mi . GetParameters ( ) [ 1 ] . Name == "predicate" ) ;
814
815
815
816
/// <summary>
816
817
/// Asynchronously returns the only element of a sequence that satisfies a specified condition or a default value if no such element exists.
@@ -864,9 +865,9 @@ public static Task<dynamic> SingleOrDefaultAsync([NotNull] this IQueryable sourc
864
865
865
866
return ExecuteAsync < dynamic > ( _singleOrDefaultPredicate , source , Expression . Quote ( lambda ) , cancellationToken ) ;
866
867
}
867
- #endregion SingleOrDefault
868
+ #endregion SingleOrDefault
868
869
869
- #region SumAsync
870
+ #region SumAsync
870
871
/// <summary>
871
872
/// Asynchronously computes the sum of a sequence of values.
872
873
/// </summary>
@@ -950,9 +951,9 @@ public static Task<dynamic> SumAsync([NotNull] this IQueryable source, Cancellat
950
951
951
952
return ExecuteDynamicAsync ( sumSelector , source , Expression . Quote ( lambda ) , cancellationToken ) ;
952
953
}
953
- #endregion SumAsync
954
+ #endregion SumAsync
954
955
955
- #region Private Helpers
956
+ #region Private Helpers
956
957
private static readonly MethodInfo _executeAsyncMethod =
957
958
typeof ( EntityFrameworkDynamicQueryableExtensions )
958
959
#if NETSTANDARD || UAP10_0
@@ -1057,8 +1058,15 @@ private static MethodInfo GetMethod<TResult>(string name, int parameterCount = 0
1057
1058
private static MethodInfo GetMethod ( string name , Type returnType , int parameterCount = 0 , Func < MethodInfo , bool > predicate = null ) =>
1058
1059
GetMethod ( name , parameterCount , mi => ( mi . ReturnType == returnType ) && ( ( predicate == null ) || predicate ( mi ) ) ) ;
1059
1060
1060
- private static MethodInfo GetMethod ( string name , int parameterCount = 0 , Func < MethodInfo , bool > predicate = null ) =>
1061
- typeof ( Queryable ) . GetTypeInfo ( ) . GetDeclaredMethods ( name ) . First ( mi => ( mi . GetParameters ( ) . Length == parameterCount + 1 ) && ( ( predicate == null ) || predicate ( mi ) ) ) ;
1061
+ private static MethodInfo GetMethod (
1062
+ string name ,
1063
+ int parameterCount = 0 ,
1064
+ Func < MethodInfo , bool > predicate = null ) =>
1065
+ typeof ( Queryable )
1066
+ . GetTypeInfo ( )
1067
+ . GetDeclaredMethods ( name )
1068
+ . Where ( mi => mi . GetParameters ( ) . Length == parameterCount + 1 )
1069
+ . First ( mi => predicate == null || predicate ( mi ) ) ;
1062
1070
1063
1071
#if EFCORE_3X
1064
1072
private static IQueryable CastSource ( IQueryable source , MethodInfo operatorMethodInfo )
@@ -1106,6 +1114,6 @@ private static Expression OptimizeExpression(Expression expression)
1106
1114
1107
1115
return expression ;
1108
1116
}
1109
- #endregion Private Helpers
1117
+ #endregion Private Helpers
1110
1118
}
1111
1119
}
0 commit comments