Ejemplo n.º 1
0
            public override IBuildContext GetContext(Expression expression, int level, BuildInfo buildInfo)
            {
                if (expression == null && buildInfo != null)
                {
                    if (buildInfo.Parent is SelectManyBuilder.SelectManyContext)
                    {
                        var sm     = (SelectManyBuilder.SelectManyContext)buildInfo.Parent;
                        var ctype  = typeof(ContextHelper <>).MakeGenericType(_key.Lambda.Parameters[0].Type);
                        var helper = (IContextHelper)Activator.CreateInstance(ctype);
                        var expr   = helper.GetContext(
                            Builder.MappingSchema,
                            Sequence.Expression,
                            _key.Lambda.Parameters[0],
                            Expression.PropertyOrField(sm.Lambda.Parameters[0], "Key"),
                            _key.Lambda.Body);

                        return(Builder.BuildSequence(new BuildInfo(buildInfo, expr)));
                    }

                    //if (buildInfo.Parent == this)
                    {
                        var ctype  = typeof(ContextHelper <>).MakeGenericType(_key.Lambda.Parameters[0].Type);
                        var helper = (IContextHelper)Activator.CreateInstance(ctype);
                        var expr   = helper.GetContext(
                            Builder.MappingSchema,
                            _sequenceExpr,
                            _key.Lambda.Parameters[0],
                            Expression.PropertyOrField(buildInfo.Expression, "Key"),
                            _key.Lambda.Body);

                        var ctx = Builder.BuildSequence(new BuildInfo(buildInfo, expr));

                        ctx.SelectQuery.Properties.Add(Tuple.Create("from_group_by", SelectQuery));

                        return(ctx);
                    }

                    //return this;
                }

                if (level != 0)
                {
                    var levelExpression = expression.GetLevelExpression(Builder.MappingSchema, level);

                    if (levelExpression.NodeType == ExpressionType.MemberAccess)
                    {
                        var ma = (MemberExpression)levelExpression;

                        if (ma.Member.Name == "Key" && ma.Member.DeclaringType == _groupingType)
                        {
                            return(ReferenceEquals(levelExpression, expression) ?
                                   _key.GetContext(null, 0, buildInfo) :
                                   _key.GetContext(expression, level + 1, buildInfo));
                        }
                    }
                }

                throw new NotImplementedException();
            }
Ejemplo n.º 2
0
            public override IBuildContext?GetContext(Expression?expression, int level, BuildInfo buildInfo)
            {
                if (expression == null && buildInfo != null)
                {
                    if (buildInfo.Parent is SelectManyBuilder.SelectManyContext sm)
                    {
                        var expr = MakeSubQueryExpression(
                            Builder.MappingSchema,
                            Sequence.Expression !,
                            _key.Lambda.Parameters[0],
                            ExpressionHelper.PropertyOrField(sm.Lambda.Parameters[0], "Key"),
                            _key.Lambda.Body);

                        return(Builder.BuildSequence(new BuildInfo(buildInfo, expr)));
                    }

                    //if (buildInfo.Parent == this)
                    {
                        var expr = MakeSubQueryExpression(
                            Builder.MappingSchema,
                            _sequenceExpr,
                            _key.Lambda.Parameters[0],
                            ExpressionHelper.PropertyOrField(buildInfo.Expression, "Key"),
                            _key.Lambda.Body);

                        var ctx = Builder.BuildSequence(new BuildInfo(buildInfo, expr));

                        ctx.SelectQuery.Properties.Add(Tuple.Create("from_group_by", SelectQuery));

                        return(ctx);
                    }

                    //return this;
                }

                if (level != 0)
                {
                    var levelExpression = expression !.GetLevelExpression(Builder.MappingSchema, level);

                    if (levelExpression.NodeType == ExpressionType.MemberAccess)
                    {
                        var ma = (MemberExpression)levelExpression;

                        if (ma.Member.Name == "Key" && ma.Member.DeclaringType == _groupingType)
                        {
                            return(ReferenceEquals(levelExpression, expression) ?
                                   _key.GetContext(null, 0, buildInfo !) :
                                   _key.GetContext(expression, level + 1, buildInfo !));
                        }
                    }
                }

                if (buildInfo != null && level == 0 && expression?.NodeType == ExpressionType.Parameter)
                {
                    var expr = MakeSubQueryExpression(
                        Builder.MappingSchema,
                        _sequenceExpr,
                        _key.Lambda.Parameters[0],
                        ExpressionHelper.PropertyOrField(buildInfo.Expression, "Key"),
                        _key.Lambda.Body);

                    expr = TypeHelper.MakeMethodCall(Methods.Enumerable.Select, expr, Element.Lambda);

                    var ctx = Builder.BuildSequence(new BuildInfo(buildInfo, expr));

                    ctx.SelectQuery.Properties.Add(Tuple.Create("from_group_by", SelectQuery));

                    return(ctx);
                }

                throw new NotImplementedException();
            }