public static DQueryable <T> SelectMany <T>(this DQueryable <T> query, CollectionElementToken cet) { Type elementType = cet.Parent.Type.ElementType(); var collectionSelector = Expression.Lambda(typeof(Func <,>).MakeGenericType(typeof(object), typeof(IEnumerable <>).MakeGenericType(elementType)), Expression.Call(miDefaultIfEmptyE.MakeGenericMethod(elementType), cet.Parent.BuildExpression(query.Context)), query.Context.Parameter); var elementParameter = cet.CreateParameter(); var properties = query.Context.Replacemens.Values.And(cet.CreateExpression(elementParameter)); var ctor = TupleReflection.TupleChainConstructor(properties); var resultSelector = Expression.Lambda(Expression.Convert(ctor, typeof(object)), query.Context.Parameter, elementParameter); var resultQuery = query.Query.Provider.CreateQuery <object>(Expression.Call(null, miSelectMany.MakeGenericMethod(typeof(object), elementType, typeof(object)), new Expression[] { query.Query.Expression, Expression.Quote(collectionSelector), Expression.Quote(resultSelector) })); var parameter = Expression.Parameter(typeof(object)); var newReplacements = query.Context.Replacemens.Keys.And(cet).Select((a, i) => new { Token = a, Expression = TupleReflection.TupleChainProperty(Expression.Convert(parameter, ctor.Type), i) }).ToDictionary(a => a.Token, a => a.Expression); return(new DQueryable <T>(resultQuery, new BuildExpressionContext(ctor.Type, parameter, newReplacements))); }
internal Func <object, PrimaryKey?> GetPrimaryKeyNullableGetter(IColumn column) { var access = TupleReflection.TupleChainProperty(Expression.Convert(originObject, tupleType), table.Columns.Values.IndexOf(column)); var primaryKey = WrapPrimaryKey(access); return(Expression.Lambda <Func <object, PrimaryKey?> >(primaryKey, originObject).Compile()); }
static Expression <Func <object, object> > TupleConstructor(BuildExpressionContext context, HashSet <QueryToken> tokens, out BuildExpressionContext newContext) { string str = tokens.Select(t => QueryUtils.CanColumn(t)).NotNull().ToString("\r\n"); if (str == null) { throw new ApplicationException(str); } List <Expression> expressions = tokens.Select(t => t.BuildExpression(context)).ToList(); Expression ctor = TupleReflection.TupleChainConstructor(expressions); var pe = Expression.Parameter(typeof(object)); newContext = new BuildExpressionContext( ctor.Type, pe, tokens.Select((t, i) => new { Token = t, Expr = TupleReflection.TupleChainProperty(Expression.Convert(pe, ctor.Type), i) }).ToDictionary(t => t.Token, t => t.Expr)); return(Expression.Lambda <Func <object, object> >( (Expression)Expression.Convert(ctor, typeof(object)), context.Parameter)); }
static LambdaExpression ResultSelectSelectorAndContext(BuildExpressionContext context, HashSet <QueryToken> keyTokens, HashSet <AggregateToken> aggregateTokens, Type keyTupleType, out BuildExpressionContext newContext) { Dictionary <QueryToken, Expression> resultExpressions = new Dictionary <QueryToken, Expression>(); ParameterExpression pk = Expression.Parameter(keyTupleType, "key"); resultExpressions.AddRange(keyTokens.Select((kt, i) => KVP.Create(kt, TupleReflection.TupleChainProperty(pk, i)))); ParameterExpression pe = Expression.Parameter(typeof(IEnumerable <object>), "e"); resultExpressions.AddRange(aggregateTokens.Select(at => KVP.Create((QueryToken)at, BuildAggregateExpression(pe, at, context)))); var resultConstructor = TupleReflection.TupleChainConstructor(resultExpressions.Values); ParameterExpression pg = Expression.Parameter(typeof(object), "gr"); newContext = new BuildExpressionContext(resultConstructor.Type, pg, resultExpressions.Keys.Select((t, i) => KVP.Create(t, TupleReflection.TupleChainProperty(Expression.Convert(pg, resultConstructor.Type), i))).ToDictionary()); return(Expression.Lambda(Expression.Convert(resultConstructor, typeof(object)), pk, pe)); }
public Expression GetTupleProperty(IColumn column) { return(TupleReflection.TupleChainProperty(origin, table.Columns.Values.IndexOf(column))); }