Ejemplo n.º 1
0
            public override Expression Translate(MethodCallExpression mce, Expression translatedSource, IEnumerable <Expression> otherArgs, QueryAnalysisContext context)
            {
                var  _otherArgs  = otherArgs.ToArray();
                bool isQueryable = DefinedOnQueryable(mce);

                //Func<TSource, int, IEnumerable<TCollection>>
                var collectionSelectorTranslation = TranslateLambdaToIEnumerableFormat(_otherArgs[0],
                                                                                       new[]   { context.QueryableType.NonPrimitiveEnumerableItemType, //TSource
                                                                                                 new SimpleType(typeof(int)) },                        //int
                                                                                       context, isQueryable);

                //Func<TSource, TCollection, TResult>
                var resultSelectorTranslation = QueryTranslationVisitor.TranslateLambda(_otherArgs[1],
                                                                                        new[] { context.QueryableType.NonPrimitiveEnumerableItemType,                         //TSource
                                                                                                collectionSelectorTranslation.QueryableType.NonPrimitiveEnumerableItemType }, //TCollection
                                                                                        context, isQueryable);

                var newArgs = new Expression[]
                {
                    translatedSource,
                    collectionSelectorTranslation.TranslatedExpression
                };

                //SelectMany<TSource, TCollection, TResult>
                var translatedMethod = mce.Method.GetGenericMethodDefinition().MakeGenericMethod(
                    context.QueryableType.NonPrimitiveEnumerableItemType.TranslatedType,
                    collectionSelectorTranslation.QueryableType.NonPrimitiveEnumerableItemType.TranslatedType,
                    resultSelectorTranslation.QueryableType.TranslatedType);

                //IEnumerable<TResult>
                context.QueryableType = RuntimeTypes.CreateEnumerable(resultSelectorTranslation.QueryableType);

                return(Expression.Call(mce.Object, translatedMethod, newArgs) as Expression);
            }
Ejemplo n.º 2
0
            public override Expression Translate(MethodCallExpression mce, Expression translatedSource, IEnumerable <Expression> otherArgs, QueryAnalysisContext context)
            {
                bool isQueryable = DefinedOnQueryable(mce);

                //Func<TSource, int, TResult>
                var selectorTranslation = QueryTranslationVisitor.TranslateLambda(otherArgs.First(),
                                                                                  new[] { context.QueryableType.NonPrimitiveEnumerableItemType, //TSource
                                                                                          new SimpleType(typeof(int)) },                        //int
                                                                                  context, isQueryable);

                var newArgs = new Expression[]
                {
                    translatedSource,
                    selectorTranslation.TranslatedExpression
                };

                //Select<TSource, TResult>
                var translatedMethod = mce.Method.GetGenericMethodDefinition().MakeGenericMethod(
                    context.QueryableType.NonPrimitiveEnumerableItemType.TranslatedType,
                    selectorTranslation.QueryableType.TranslatedType);

                context.QueryableType = RuntimeTypes.CreateEnumerable(selectorTranslation.QueryableType);

                return(Expression.Call(mce.Object, translatedMethod, newArgs));
            }
Ejemplo n.º 3
0
        public virtual IEnumerable <T> GetResultList(Expression expression)
        {
            var visitor = new QueryTranslationVisitor(new QueryAnalysisContext(entityContext._InternalServices.TypeTranslationUtil)
            {
                ModifyProjection = true
            });
            var translationResults = visitor.GetTranslatedResults(expression);

            var tableEntityQuery = translationResults.AnalysisContext.RootEntityQuery.TableEntityQuery
                                   .Provider.CreateQuery(translationResults.TranslatedExpression);

            if (TypesUtil.IsPrimitiveDataType(typeof(T)))
            {
                return(tableEntityQuery as IEnumerable <T>);
            }
            else
            {
                var tableEntities = new List <object>();
                foreach (object o in tableEntityQuery)
                {
                    tableEntities.Add(o);
                }

                return(MakeResultList(tableEntities, translationResults.AnalysisContext.QueryableType.NonPrimitiveEnumerableItemType));
            }
        }
Ejemplo n.º 4
0
            public override Expression Translate(MethodCallExpression mce, Expression translatedSource, IEnumerable <Expression> otherArgs, QueryAnalysisContext context)
            {
                var  _otherArgs  = otherArgs.ToArray();
                bool isQueryable = DefinedOnQueryable(mce);

                //Func<TSource, TKey>
                var keySelectorTranslation = QueryTranslationVisitor.TranslateLambda(_otherArgs[0], new[] { context.QueryableType.NonPrimitiveEnumerableItemType }, context, isQueryable);

                //Func<TKey, IEnumerable<TSource>, TResult>
                var resultSelectorTranslation = QueryTranslationVisitor.TranslateLambda(_otherArgs[1], new[] { keySelectorTranslation.QueryableType, context.QueryableType }, context, isQueryable);

                var newArgs = new Expression[]
                {
                    translatedSource,
                    keySelectorTranslation.TranslatedExpression,
                    resultSelectorTranslation.TranslatedExpression
                };

                //GroupBy2<TSource, TKey, TResult>
                var translatedMethod = mce.Method.GetGenericMethodDefinition().MakeGenericMethod(
                    context.QueryableType.NonPrimitiveEnumerableItemType.TranslatedType,
                    keySelectorTranslation.QueryableType.TranslatedType,
                    resultSelectorTranslation.QueryableType.TranslatedType);

                //IQueryable<TResult>
                context.QueryableType = RuntimeTypes.CreateEnumerable(resultSelectorTranslation.QueryableType);

                return(Expression.Call(mce.Object, translatedMethod, newArgs));
            }
Ejemplo n.º 5
0
            public override Expression Translate(MethodCallExpression mce, Expression translatedSource, IEnumerable <Expression> otherArgs, QueryAnalysisContext context)
            {
                bool isQueryable = DefinedOnQueryable(mce);

                //Func<TSource, TKey>
                var keySelectorTranslation = QueryTranslationVisitor.TranslateLambda(otherArgs.First(),
                                                                                     new[] { context.QueryableType.NonPrimitiveEnumerableItemType }, context, isQueryable);

                var newArgs = new Expression[]
                {
                    translatedSource,
                    keySelectorTranslation.TranslatedExpression
                };

                //GroupBy<TSource, TKey>
                var translatedMethod = mce.Method.GetGenericMethodDefinition().MakeGenericMethod(
                    context.QueryableType.NonPrimitiveEnumerableItemType.TranslatedType,
                    keySelectorTranslation.QueryableType.TranslatedType);

                //IQueryable<IGrouping<TKey, TSource>>
                var groupingType  = RuntimeTypes.CreateGrouping(keySelectorTranslation.QueryableType, context.QueryableType.NonPrimitiveEnumerableItemType);
                var queryableType = RuntimeTypes.CreateEnumerable(groupingType);

                context.QueryableType = queryableType;

                return(Expression.Call(mce.Object, translatedMethod, newArgs));
            }
Ejemplo n.º 6
0
        /// <inheritdoc/>
        public Func <TParam1, TResult> Compile <TParam1, TResult>(Expression expression, ParameterExpression param1)
        {
            var visitor = new QueryTranslationVisitor();

            expression = visitor.Visit(expression);
            var query = Expression.Lambda <Func <TParam1, TResult> >(expression, param1);

            return(query.Compile());
        }
Ejemplo n.º 7
0
        /// <inheritdoc/>
        public Func <T> Compile <T>(Expression expression)
        {
            var visitor = new QueryTranslationVisitor();

            expression = visitor.Visit(expression);
            var query = Expression.Lambda <Func <T> >(expression);

            return(query.Compile());
        }
Ejemplo n.º 8
0
        protected ExpressionTranslationResult TranslateLambdaToIEnumerableFormat(Expression lambdaArg, SimpleType[] parameterTypes, QueryAnalysisContext context, bool isQueryable)
        {
            var lambda      = QueryTranslationVisitor.GetLambda(lambdaArg, isQueryable);
            var translation = QueryTranslationVisitor.TranslateLambda(lambda, parameterTypes, context);

            //select many requires enumerable return type in collection selector lambda delegate.
            //hence we need convert to IEnumerable<>
            var translatedLambda    = translation.TranslatedExpression as LambdaExpression;
            var translatedLambdaArg = QueryTranslationVisitor.FormatLambda(GetTranslatedLambda
                                                                               (translatedLambda.Type, translatedLambda.Body, translatedLambda.Parameters), isQueryable);

            return(new ExpressionTranslationResult(translatedLambdaArg, translation.QueryableType));
        }
Ejemplo n.º 9
0
        private ExpressionTranslationResult GetTranslationResults()
        {
            var translatableLambda = Expression.Lambda(materializableExpression, originalExpression.Parameters.ToArray());

            //Add the other Params
            var parameters = originalExpression.Parameters
                             .Select(p => new SimpleType(p.Type, typeTranslationUtil.GetTranslatedType(p.Type))).ToArray();

            var translationResults = QueryTranslationVisitor.TranslateLambda(translatableLambda, parameters.ToArray(),
                                                                             new QueryAnalysisContext(typeTranslationUtil), false);

            return(translationResults);
        }
Ejemplo n.º 10
0
        public override Expression Translate(MethodCallExpression mce, Expression translatedSource1, IEnumerable <Expression> otherArgs, QueryAnalysisContext context)
        {
            bool isQueryable = DefinedOnQueryable(mce);

            var translation = QueryTranslationVisitor.TranslateLambda(otherArgs.First(), new[] { context.QueryableType.NonPrimitiveEnumerableItemType }, context, isQueryable);

            var newArgs = new Expression[]
            {
                translatedSource1,
                translation.TranslatedExpression
            };

            var translatedMethod = mce.Method.GetGenericMethodDefinition().MakeGenericMethod(context.QueryableType.NonPrimitiveEnumerableItemType.TranslatedType);

            return(Expression.Call(mce.Object, translatedMethod, newArgs));
        }
Ejemplo n.º 11
0
            public override Expression Translate(MethodCallExpression mce, Expression translatedSource1, IEnumerable <System.Linq.Expressions.Expression> otherArgs, QueryAnalysisContext context)
            {
                var translatedSource2 = QueryTranslationVisitor.VisitWithNewVisitor(otherArgs.Single(), context, false).TranslatedExpression;

                if (translatedSource1.Type != translatedSource2.Type)
                {
                    throw new Exception("Intersecting queries must be of the same shape.");
                }

                var newArgs = new Expression[]
                {
                    translatedSource1,
                    translatedSource2
                };

                var translatedMethod = mce.Method.GetGenericMethodDefinition().MakeGenericMethod(context.QueryableType.NonPrimitiveEnumerableItemType.TranslatedType);

                return(Expression.Call(mce.Object, translatedMethod, newArgs));
            }
Ejemplo n.º 12
0
            public override Expression Translate(MethodCallExpression mce, Expression translatedSource1, IEnumerable <Expression> otherArgs, QueryAnalysisContext context)
            {
                var translatedSource2 = QueryTranslationVisitor.VisitWithNewVisitor(otherArgs.Single(), context, context.ModifyProjection).TranslatedExpression;

                if (translatedSource1.Type != translatedSource2.Type)
                {
                    throw new Exception("Queries joined with a Union must be similar.");
                }

                var newArgs = new Expression[]
                {
                    translatedSource1,
                    translatedSource2
                };

                var translatedMethod = mce.Method.GetGenericMethodDefinition().MakeGenericMethod(context.QueryableType.NonPrimitiveEnumerableItemType.TranslatedType);

                return(Expression.Call(mce.Object, translatedMethod, newArgs));
            }
Ejemplo n.º 13
0
        public virtual T GetResult(Expression expression)
        {
            var visitor = new QueryTranslationVisitor(new QueryAnalysisContext(entityContext._InternalServices.TypeTranslationUtil)
            {
                ModifyProjection = true
            });
            var translationResults = visitor.GetTranslatedResults(expression);

            var tableEntityQuery = translationResults.AnalysisContext.RootEntityQuery.TableEntityQuery;
            var tableEntity      = tableEntityQuery.Provider.Execute(translationResults.TranslatedExpression);

            if (TypesUtil.IsPrimitiveDataType(typeof(T)))
            {
                return((T)tableEntity);
            }
            else
            {
                return(MakeResult(tableEntity, translationResults.AnalysisContext.QueryableType));
            }
        }
Ejemplo n.º 14
0
            public override Expression Translate(MethodCallExpression mce, Expression translatedOuter, IEnumerable <Expression> otherArgs, QueryAnalysisContext context)
            {
                bool isQueryable = DefinedOnQueryable(mce);

                var _otherArgs = otherArgs.ToArray();

                //IEnumerable<TInner>
                var innerTranslation = QueryTranslationVisitor.VisitWithNewVisitor(_otherArgs[0], context, false);

                //Func<TOuter, TKey>
                var outerKeySelectorTranslation = QueryTranslationVisitor.TranslateLambda(_otherArgs[1], new[] { context.QueryableType.NonPrimitiveEnumerableItemType }, context, isQueryable);

                //Func<TInner, TKey>
                var innerKeySelectorTranslation = QueryTranslationVisitor.TranslateLambda(_otherArgs[2], new[] { innerTranslation.QueryableType.NonPrimitiveEnumerableItemType }, context, isQueryable);

                //Func<TOuter, TInner, TResult>
                var resultSelectorTranslation = QueryTranslationVisitor.TranslateLambda(_otherArgs[3], new[] { context.QueryableType.NonPrimitiveEnumerableItemType, innerTranslation.QueryableType.NonPrimitiveEnumerableItemType }, context, isQueryable);

                var newArgs = new Expression[]
                {
                    translatedOuter,
                    innerTranslation.TranslatedExpression,
                    outerKeySelectorTranslation.TranslatedExpression,
                    innerKeySelectorTranslation.TranslatedExpression,
                    resultSelectorTranslation.TranslatedExpression
                };

                //Join<TOuter, TInner, TKey, TResult>
                var translatedMethod = mce.Method.GetGenericMethodDefinition().MakeGenericMethod(
                    context.QueryableType.NonPrimitiveEnumerableItemType.TranslatedType,
                    innerTranslation.QueryableType.NonPrimitiveEnumerableItemType.TranslatedType,
                    outerKeySelectorTranslation.QueryableType.TranslatedType,
                    resultSelectorTranslation.QueryableType.TranslatedType);

                context.QueryableType = RuntimeTypes.CreateEnumerable(resultSelectorTranslation.QueryableType);

                return(Expression.Call(mce.Object, translatedMethod, newArgs));
            }
Ejemplo n.º 15
0
            public override Expression Translate(MethodCallExpression mce, Expression translatedSource1, IEnumerable <Expression> otherArgs, QueryAnalysisContext context)
            {
                if (context.QueryableType.NonPrimitiveEnumerableItemType is ProjectedType)
                {
                    throw new NotSupportedException("Contains cannot be called after projection.");
                }

                var translatedSource2 = QueryTranslationVisitor.VisitWithNewVisitor(otherArgs.Single(), context, false).TranslatedExpression;
                var newArgs           = new Expression[]
                {
                    translatedSource1,
                    translatedSource2
                };

                //If NonPrimitiveEnumerableItemType is not null (case 1), this means we have to change the method's generic args.
                //Otherwise, re-use the same (case 2).
                //The second case occurs when we have a List<int> (for example), which has NonPrimitiveEnumerableItemType = null.
                var translatedMethod = context.QueryableType.NonPrimitiveEnumerableItemType != null?
                                       mce.Method.GetGenericMethodDefinition().MakeGenericMethod(context.QueryableType.NonPrimitiveEnumerableItemType.TranslatedType)
                                           : mce.Method;

                context.QueryableType = new SimpleType(typeof(bool), typeof(bool));
                return(Expression.Call(mce.Object, translatedMethod, newArgs));
            }