/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public CompiledQuery(LambdaExpression queryExpression) { Type contextType; switch (queryExpression.Body) { case MethodCallExpression methodCall: contextType = (methodCall.Arguments[0] as MethodCallExpression)?.Type.GenericTypeArguments[0]; break; default: throw new NotSupportedException(); } if (contextType == null) { var q = new DummyQueryContext <T>(); SqlStatement = q.GetQuery(queryExpression.Body); } else { var queryContextType = typeof(DummyQueryContext <>).MakeGenericType(contextType); object q = Activator.CreateInstance(queryContextType); var methodInfo = queryContextType.GetMethod(nameof(DummyQueryContext <object> .GetQuery)); if (methodInfo == null) { throw new MissingMethodException(); } SqlStatement = methodInfo.Invoke(q, new object[] { queryExpression.Body }) as string; } }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public CompiledQuery(LambdaExpression queryExpression, IQueryExecutor queryExecutor) { QueryExecutor = queryExecutor ?? throw new ArgumentNullException(nameof(queryExecutor)); Type contextType; switch (queryExpression.Body) { case MethodCallExpression methodCall: if (methodCall.Arguments.Any() && methodCall.Arguments[0] is MethodCallExpression methodCallExpression) { contextType = methodCallExpression.Type.GenericTypeArguments[0]; } else { contextType = methodCall.Object?.Type.GenericTypeArguments.FirstOrDefault() ?? methodCall.Type; } break; default: throw new NotSupportedException(); } TranslatedQuery translatedQuery; if (contextType == null) { var q = new DummyQueryContext <T>(); translatedQuery = q.GetQuery(queryExpression.Body); } else { var queryContextType = typeof(DummyQueryContext <>).MakeGenericType(contextType); var q = Activator.CreateInstance(queryContextType); var methodInfo = queryContextType.GetMethod(nameof(DummyQueryContext <object> .GetQuery)); if (methodInfo == null) { throw new MissingMethodException(); } translatedQuery = (TranslatedQuery)methodInfo.Invoke(q, new object[] { queryExpression.Body }); } SqlStatement = translatedQuery.SqlStatement; QueryResultMethod = translatedQuery.ResultMethod; }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public CompiledQuery(LambdaExpression queryExpression, IQueryExecutor queryExecutor) { QueryExecutor = queryExecutor ?? throw new ArgumentNullException(nameof(queryExecutor)); Type contextType; if (queryExpression.Body is MethodCallExpression methodCall) { var rootMethodCall = GetRootMethodCall(methodCall); if (methodCall != rootMethodCall) { contextType = rootMethodCall.Type.GenericTypeArguments[0]; } else { contextType = rootMethodCall.Object?.Type.GenericTypeArguments.FirstOrDefault() ?? rootMethodCall.Type; } } else { throw new NotSupportedException(); } if (contextType == null) { var q = new DummyQueryContext <T>(); TranslatedQuery = q.GetQuery(queryExpression.Body); } else { var queryContextType = typeof(DummyQueryContext <>).MakeGenericType(contextType); object q = Activator.CreateInstance(queryContextType); var methodInfo = queryContextType.GetMethod(nameof(DummyQueryContext <object> .GetQuery)); if (methodInfo == null) { throw new MissingMethodException(); } TranslatedQuery = (TranslatedQuery)methodInfo.Invoke(q, new object[] { queryExpression.Body }); } }