public static ISharedStoredProcedure CreateSharedStoredProcedure( LambdaExpression query) { if (!query.Type.IsGenericType || query.Type.GetGenericTypeDefinition() != typeof(Func <,>)) { throw new ArgumentException("Invalid query", "query"); } Type[] queryArgs = query.Type.GetGenericArguments(); if (queryArgs[0] != typeof(IDatabase)) { throw new ArgumentException("Invalid query", "query"); } Type queryType = queryArgs[1]; if (!queryType.IsGenericType || queryType.GetGenericTypeDefinition() != typeof(IQueryable <>)) { throw new ArgumentException("Not IQueryable<>", "query"); } Type entityType = TypeHelper.GetElementType(queryType); ISharedStoredProcedure procedure = typeof(DatabaseReflectionHelper.WrapperMethods) .GetMethod("CreateSharedStoredProcedure") .MakeGenericMethod(entityType) .Invoke(null, new object[] { query }) as ISharedStoredProcedure; return(procedure); }
public DbDataReader ExecuteDataReader(ActionContext context) { TransformVisitor visitor = new TransformVisitor(context.DbContainer.TypeConverter); visitor.TableProvider = context.DbContainer; // Transform command tree Expression queryExpression = visitor.Visit(this.commandTree.Query); LambdaExpression query = Expression.Lambda(queryExpression, Expression.Parameter(typeof(IDatabase))); // Create a stored procedure from the expression ISharedStoredProcedure procedure = DatabaseReflectionHelper.CreateSharedStoredProcedure(query); // Format the parameter values IDictionary <string, object> parameters = DbCommandActionHelper.FormatParameters( context.Parameters, procedure.Parameters, context.DbContainer.TypeConverter); IEnumerable result = null; if (context.Transaction != null) { result = procedure.Execute( context.DbContainer.Internal, parameters, context.Transaction); } else { result = procedure.Execute( context.DbContainer.Internal, parameters); } List <FieldDescription> fields = GetReturningFields(this.commandTree); return(new EffortDataReader( result, -1, fields.ToArray(), context.DbContainer)); }