1- using System . ComponentModel . DataAnnotations ;
1+ using AutoMapper ;
2+ using AutoMapper . Internal ;
3+ using AutoMapper . QueryableExtensions ;
4+ using Microsoft . EntityFrameworkCore ;
5+ using Microsoft . EntityFrameworkCore . Infrastructure ;
6+ using Swashbuckle . AspNetCore . SwaggerGen ;
27using System . Globalization ;
38using System . Linq . Expressions ;
49using System . Reflection ;
510using System . Reflection . Emit ;
611using System . Text . Json ;
712using System . Text . Json . Nodes ;
813using System . Text . Json . Serialization ;
9- using AutoMapper ;
10- using AutoMapper . Internal ;
11- using AutoMapper . QueryableExtensions ;
12- using Microsoft . EntityFrameworkCore ;
13- using Microsoft . EntityFrameworkCore . Infrastructure ;
14- using Swashbuckle . AspNetCore . SwaggerGen ;
1514
1615namespace Infragistics . QueryBuilder . Executor
1716{
@@ -34,20 +33,6 @@ public static object[] Run<TSource, TTarget>(this IQueryable<TSource> source, Qu
3433 return db is not null ? BuildQuery < TSource , TTarget > ( db , source , query , mapper ) . ToArray ( ) : Array . Empty < object > ( ) ;
3534 }
3635
37- public static object [ ] InvokeRunMethod ( Type [ ] genericArgs , object ? [ ] parameters )
38- {
39- var method = typeof ( QueryExecutor )
40- . GetMethods ( BindingFlags . Static | BindingFlags . Public )
41- . FirstOrDefault ( m =>
42- m . Name == "Run" &&
43- m . IsGenericMethodDefinition &&
44- m . GetGenericArguments ( ) . Length == genericArgs . Length )
45- ? . MakeGenericMethod ( genericArgs ) ;
46-
47- var result = method ? . Invoke ( null , parameters ) ?? Array . Empty < object > ( ) ;
48- return ( object [ ] ) result ;
49- }
50-
5136 private static IQueryable < object > BuildQuery < TSource , TTarget > ( DbContext db , IQueryable < TSource > source , Query ? query , IMapper ? mapper = null )
5237 {
5338 if ( query is null )
@@ -237,18 +222,9 @@ private static Expression BuildInExpression<T>(DbContext db, Query? query, Membe
237222
238223 private static IEnumerable < dynamic > RunSubquery ( DbContext db , Query ? query )
239224 {
240- var propName = query ? . Entity . ToLower ( CultureInfo . InvariantCulture ) ?? string . Empty ;
241- var prop = db . GetType ( ) . GetProperty ( propName , BindingFlags . IgnoreCase | BindingFlags . Public | BindingFlags . Instance )
242- ?? throw new InvalidOperationException ( $ "Property '{ propName } ' not found on type '{ db . GetType ( ) } '") ;
243-
244- var methods = typeof ( QueryExecutor ) . GetMethods ( BindingFlags . Static | BindingFlags . Public ) ;
245- var method = methods ? . FirstOrDefault ( m => m . CustomAttributes . Count ( ) == 1 ) ;
246- var dbSet = prop . GetValue ( db ) ?? throw new ValidationException ( $ "DbSet property '{ prop . Name } ' is null in DbContext.") ;
247- var genericType = prop . PropertyType . GetGenericArguments ( ) . FirstOrDefault ( ) ?? throw new ValidationException ( $ "Missing DbSet generic type") ;
248- var queryable = dbSet ? . GetType ( ) . GetMethod ( "AsQueryable" ) ? . Invoke ( dbSet , null ) ;
249-
250- return InvokeRunMethod ( [ genericType ] , [ queryable , query ] ) ;
251-
225+ var t = query ? . Entity . ToLower ( CultureInfo . InvariantCulture ) ?? string . Empty ;
226+ var p = db . GetType ( ) . GetProperty ( t , BindingFlags . IgnoreCase | BindingFlags . Public | BindingFlags . Instance ) ?? throw new InvalidOperationException ( $ "Property '{ t } ' not found on type '{ db . GetType ( ) } '") ;
227+ return p . GetValue ( db ) is not IQueryable < dynamic > q ? Array . Empty < dynamic > ( ) : [ .. q . Run ( query ) ] ;
252228 }
253229
254230 private static dynamic ? ProjectField ( object ? obj , string field )
@@ -266,29 +242,13 @@ private static Expression GetSearchValue(JsonValue? jsonVal, Type targetType)
266242 }
267243
268244 var nonNullableType = Nullable . GetUnderlyingType ( targetType ) ?? targetType ;
245+ var value = jsonVal . Deserialize ( targetType ) ;
269246
270- if ( nonNullableType . IsEnum )
247+ if ( nonNullableType . IsEnum && value is string )
271248 {
272- if ( valueKind == JsonValueKind . String )
273- {
274- var enumValue = jsonVal . Deserialize < string > ( ) ;
275- if ( enumValue != null )
276- {
277- return Expression . Constant ( Enum . Parse ( nonNullableType , enumValue ) ) ;
278- }
279- }
280- else if ( valueKind == JsonValueKind . Number )
281- {
282- var enumValue = jsonVal . Deserialize < int ? > ( ) ;
283- if ( enumValue != null )
284- {
285- return Expression . Constant ( Enum . ToObject ( nonNullableType , enumValue ) ) ;
286- }
287- }
249+ return Expression . Constant ( Enum . Parse ( nonNullableType , ( string ) value ) ) ;
288250 }
289251
290- var value = jsonVal . Deserialize ( targetType ) ;
291-
292252 var convertedValue = Convert . ChangeType ( value , nonNullableType , CultureInfo . InvariantCulture ) ;
293253 return Expression . Constant ( convertedValue , targetType ) ;
294254 }
0 commit comments