@@ -126,40 +126,30 @@ private static Expression BuildConditionExpression<TEntity>(DataContext db, IQue
126126
127127 private static Expression BuildInExpression ( DataContext db , Query ? query , MemberExpression field )
128128 {
129- if ( field . Type == typeof ( string ) )
130- {
131- var d = RunSubquery ( db , query ) . Select ( x => ( string ) ProjectField ( x , query ? . ReturnFields [ 0 ] ?? string . Empty ) ) . ToArray ( ) ;
132- return Expression . Call ( typeof ( Enumerable ) , "Contains" , new [ ] { typeof ( string ) } , Expression . Constant ( d ) , field ) ;
133- }
134- else if ( field . Type == typeof ( bool ) || field . Type == typeof ( bool ? ) )
135- {
136- var d = RunSubquery ( db , query ) . Select ( x => ( bool ? ) ProjectField ( x , query ? . ReturnFields [ 0 ] ?? string . Empty ) ) . ToArray ( ) ;
137- return Expression . Call ( typeof ( Enumerable ) , "Contains" , new [ ] { typeof ( bool ? ) } , Expression . Constant ( d ) , field ) ;
138- }
139- else if ( field . Type == typeof ( int ) || field . Type == typeof ( int ? ) )
140- {
141- var d = RunSubquery ( db , query ) . Select ( x => ( int ? ) ProjectField ( x , query ? . ReturnFields [ 0 ] ?? string . Empty ) ) . ToArray ( ) ;
142- return Expression . Call ( typeof ( Enumerable ) , "Contains" , new [ ] { typeof ( int ? ) } , Expression . Constant ( d ) , field ) ;
143- }
144- else if ( field . Type == typeof ( decimal ) || field . Type == typeof ( decimal ? ) )
145- {
146- var d = RunSubquery ( db , query ) . Select ( x => ( decimal ? ) ProjectField ( x , query ? . ReturnFields [ 0 ] ?? string . Empty ) ) . ToArray ( ) ;
147- return Expression . Call ( typeof ( Enumerable ) , "Contains" , new [ ] { typeof ( decimal ? ) } , Expression . Constant ( d ) , field ) ;
148- }
149- else if ( field . Type == typeof ( float ) || field . Type == typeof ( float ? ) )
150- {
151- var d = RunSubquery ( db , query ) . Select ( x => ( float ? ) ProjectField ( x , query ? . ReturnFields [ 0 ] ?? string . Empty ) ) . ToArray ( ) ;
152- return Expression . Call ( typeof ( Enumerable ) , "Contains" , new [ ] { typeof ( float ? ) } , Expression . Constant ( d ) , field ) ;
153- }
154- else if ( field . Type == typeof ( DateTime ) || field . Type == typeof ( DateTime ? ) )
155- {
156- var d = RunSubquery ( db , query ) . Select ( x => ( DateTime ? ) ProjectField ( x , query ? . ReturnFields [ 0 ] ?? string . Empty ) ) . ToArray ( ) ;
157- return Expression . Call ( typeof ( Enumerable ) , "Contains" , new [ ] { typeof ( DateTime ) } , Expression . Constant ( d ) , field ) ;
158- }
159- else
160- {
161- throw new InvalidOperationException ( $ "Type '{ field . Type } ' not supported for 'IN' operation") ;
162- }
129+ return field . Type switch
130+ {
131+ { } t when t == typeof ( string ) => BuildInExpression < string > ( db , query , field ) ,
132+ { } t when t == typeof ( bool ) => BuildInExpression < bool > ( db , query , field ) ,
133+ { } t when t == typeof ( bool ? ) => BuildInExpression < bool ? > ( db , query , field ) ,
134+ { } t when t == typeof ( int ) => BuildInExpression < int > ( db , query , field ) ,
135+ { } t when t == typeof ( int ? ) => BuildInExpression < int ? > ( db , query , field ) ,
136+ { } t when t == typeof ( decimal ) => BuildInExpression < decimal > ( db , query , field ) ,
137+ { } t when t == typeof ( decimal ? ) => BuildInExpression < decimal ? > ( db , query , field ) ,
138+ { } t when t == typeof ( float ) => BuildInExpression < float > ( db , query , field ) ,
139+ { } t when t == typeof ( float ? ) => BuildInExpression < float ? > ( db , query , field ) ,
140+ { } t when t == typeof ( DateTime ) => BuildInExpression < DateTime > ( db , query , field ) ,
141+ { } t when t == typeof ( DateTime ? ) => BuildInExpression < DateTime ? > ( db , query , field ) ,
142+ _ => throw new InvalidOperationException ( $ "Type '{ field . Type } ' not supported for 'IN' operation") ,
143+ } ;
144+ }
145+
146+ private static Expression BuildInExpression < T > ( DataContext db , Query ? query , MemberExpression field )
147+ {
148+ var d = RunSubquery ( db , query ) . Select ( x => ( T ) ProjectField ( x , query ? . ReturnFields [ 0 ] ?? string . Empty ) ) . ToArray ( ) ;
149+ var m = typeof ( Enumerable ) . GetMethods ( )
150+ . FirstOrDefault ( method => method . Name == nameof ( Enumerable . Contains ) && method . GetParameters ( ) . Length == 2 )
151+ ? . MakeGenericMethod ( typeof ( T ) ) ?? throw new InvalidOperationException ( "Missing method" ) ;
152+ return Expression . Call ( m , Expression . Constant ( d ) , field ) ;
163153 }
164154
165155 private static IEnumerable < dynamic > RunSubquery ( DataContext db , Query ? query )
0 commit comments