예제 #1
0
        /// <summary>
        /// Creates an <see cref="ImperativeQueryable{T}"/> from an array.
        /// </summary>
        /// <typeparam name="T">The element type.</typeparam>
        /// <param name="arr">The array.</param>
        /// <returns>The queryable object.</returns>
        public static ImperativeQueryable <T> AsImperativeQueryable <T>(this T[] arr)
        {
            var arrExpr    = Expression.Constant(arr);
            var expression = EnumerableExpressionExtensions.OfArray(arrExpr, typeof(T));

            return(Create <T>(expression));
        }
예제 #2
0
        /// <summary>
        /// Creates an <see cref="ImperativeQueryable{T}"/> from an <see cref="IEnumerable{T}"/>.
        /// </summary>
        /// <typeparam name="T">The element type.</typeparam>
        /// <param name="enumerable">The enumerable.</param>
        /// <returns>The queryable object.</returns>
        public static ImperativeQueryable <T> AsImperativeQueryable <T>(this IEnumerable <T> enumerable)
        {
            var enumerableExpr = Expression.Constant(enumerable);
            var expression     = EnumerableExpressionExtensions.OfEnumerable(enumerableExpr, typeof(T));

            return(Create <T>(expression));
        }
            /// <inheritdoc/>
            protected override Expression VisitMethodCall(MethodCallExpression node)
            {
                if (node.Method.DeclaringType == typeof(Queryable))
                {
                    Expression source = this.Visit(node.Arguments[0]);
                    if (source is EnumerableExpression enumerableExpression)
                    {
                        switch (node.Method.Name)
                        {
                        case "Aggregate" when node.Arguments.Count == 3:
                        {
                            Expression       seed = this.Visit(node.Arguments[1]);
                            LambdaExpression func = Unquote(this.Visit(node.Arguments[2]));
                            return(enumerableExpression.Aggregate(func, seed));
                        }

                        case "Where" when node.Arguments.Count == 2:
                        {
                            LambdaExpression predicate = Unquote(this.Visit(node.Arguments[1]));
                            return(enumerableExpression.Where(predicate));
                        }

                        case "Select" when node.Arguments.Count == 2:
                        {
                            LambdaExpression selector = Unquote(this.Visit(node.Arguments[1]));
                            return(enumerableExpression.Select(selector));
                        }

                        case "SelectMany" when node.Arguments.Count == 2:
                        {
                            LambdaExpression selector = Unquote(this.Visit(node.Arguments[1]));
                            return(enumerableExpression.SelectMany(selector));
                        }
                        }
                    }
                }
                else if (node.Method.DeclaringType == typeof(ImperativeQueryableExtensions))
                {
                    if (node.Method.Name == nameof(ImperativeQueryableExtensions.AsImperativeQueryable))
                    {
                        var firstArg = node.Arguments[0];
                        if (firstArg.Type.IsArray)
                        {
                            return(EnumerableExpressionExtensions.OfArray(firstArg));
                        }
                        else
                        {
                            return(EnumerableExpressionExtensions.OfEnumerable(firstArg));
                        }
                    }
                }

                return(base.VisitMethodCall(node));
            }