@@ -11,11 +11,11 @@ namespace IQueryableObjectSource;
1111
1212public static class SqlExtensions
1313{
14- private static object Private ( this object obj , string privateField )
14+ private static object ? Private ( this object obj , string privateField )
1515 => obj ? . GetType ( ) . GetField ( privateField , BindingFlags . Instance | BindingFlags . NonPublic ) ? . GetValue ( obj ) ;
1616
17- private static T Private < T > ( this object obj , string privateField )
18- => ( T ) obj ? . GetType ( ) . GetField ( privateField , BindingFlags . Instance | BindingFlags . NonPublic ) ? . GetValue ( obj ) ;
17+ private static T ? Private < T > ( this object obj , string privateField )
18+ => ( T ? ) obj ? . GetType ( ) . GetField ( privateField , BindingFlags . Instance | BindingFlags . NonPublic ) ? . GetValue ( obj ) ;
1919
2020 /// <summary>
2121 /// Gets a SQL statement from an IQueryable
@@ -26,11 +26,34 @@ public static string ToQueryString(this IQueryable query)
2626 {
2727 var enumerator = query . Provider . Execute < IEnumerable > ( query . Expression ) . GetEnumerator ( ) ;
2828 var relationalCommandCache = enumerator . Private ( "_relationalCommandCache" ) ;
29+
30+ if ( relationalCommandCache == null )
31+ {
32+ return string . Empty ;
33+ }
34+
2935 var selectExpression = relationalCommandCache . Private < SelectExpression > ( "_selectExpression" ) ;
3036 var factory = relationalCommandCache . Private < IQuerySqlGeneratorFactory > ( "_querySqlGeneratorFactory" ) ;
37+
38+ if ( factory == null )
39+ {
40+ return string . Empty ;
41+ }
42+
3143 var relationalQueryContext = enumerator . Private < RelationalQueryContext > ( "_relationalQueryContext" ) ;
44+
45+ if ( relationalQueryContext == null )
46+ {
47+ return string . Empty ;
48+ }
49+
3250 var parameterValueBasedSelectExpressionOptimizer = relationalCommandCache . Private < ParameterValueBasedSelectExpressionOptimizer > ( "_parameterValueBasedSelectExpressionOptimizer" ) ;
3351
52+ if ( parameterValueBasedSelectExpressionOptimizer == null )
53+ {
54+ return string . Empty ;
55+ }
56+
3457 ( selectExpression , _ ) = parameterValueBasedSelectExpressionOptimizer . Optimize ( selectExpression , relationalQueryContext . ParameterValues ) ;
3558
3659 var sqlGenerator = factory . Create ( ) ;
@@ -59,7 +82,9 @@ private static string GetActualValue(object value)
5982 var type = value . GetType ( ) ;
6083
6184 if ( type . IsNumeric ( ) )
62- return value . ToString ( ) ;
85+ {
86+ return value . ToString ( ) ?? string . Empty ;
87+ }
6388
6489 if ( type == typeof ( DateTime ) || type == typeof ( DateTimeOffset ) )
6590 {
@@ -76,21 +101,22 @@ private static string GetActualValue(object value)
76101 return $ "'{ value } '";
77102 }
78103
79- private static bool IsNullable ( this Type type )
80- {
81- return
82- type != null &&
83- type . IsGenericType &&
84- type . GetGenericTypeDefinition ( ) == typeof ( Nullable < > ) ;
85- }
104+ private static bool IsNullable ( this Type ? type )
105+ => type != null &&
106+ type . IsGenericType &&
107+ type . GetGenericTypeDefinition ( ) == typeof ( Nullable < > ) ;
86108
87- private static bool IsNumeric ( this Type type )
109+ private static bool IsNumeric ( this Type ? type )
88110 {
89111 if ( IsNullable ( type ) )
90- type = Nullable . GetUnderlyingType ( type ) ;
112+ {
113+ type = Nullable . GetUnderlyingType ( type ! ) ;
114+ }
91115
92116 if ( type == null || type . IsEnum )
117+ {
93118 return false ;
119+ }
94120
95121 return Type . GetTypeCode ( type ) switch
96122 {
0 commit comments