Пример #1
0
        public static IQueryable ApplyTake(IQueryable source, Type elementType, int takeCount)
        {
            var method = TypeFns.GetMethodByExample((IQueryable <String> q) => Queryable.Take <String>(q, 999), elementType);
            var func   = BuildIQueryableFunc(elementType, method, takeCount);

            return(func(source));
        }
Пример #2
0
        private static MethodInfo GetOrderByMethod(bool isThenBy, bool isDesc, Type elementType, Type nextExprType)
        {
            MethodInfo orderByMethod;

            if (isThenBy)
            {
                orderByMethod = isDesc
                          ? TypeFns.GetMethodByExample(
                    (IOrderedQueryable <String> q) => q.ThenByDescending(s => s.Length),
                    elementType, nextExprType)
                          : TypeFns.GetMethodByExample(
                    (IOrderedQueryable <String> q) => q.ThenBy(s => s.Length),
                    elementType, nextExprType);
            }
            else
            {
                orderByMethod = isDesc
                          ? TypeFns.GetMethodByExample(
                    (IQueryable <String> q) => q.OrderByDescending(s => s.Length),
                    elementType, nextExprType)
                          : TypeFns.GetMethodByExample(
                    (IQueryable <String> q) => q.OrderBy(s => s.Length),
                    elementType, nextExprType);
            }
            return(orderByMethod);
        }
Пример #3
0
        public static IQueryable ApplySkip(IQueryable source, Type elementType, int skipCount)
        {
            var method = TypeFns.GetMethodByExample((IQueryable <string> q) => Queryable.Skip <string>(q, 999), elementType);
            var func   = BuildIQueryableFunc(elementType, method, skipCount);

            return(func(source));
        }
Пример #4
0
        public static IQueryable ApplyWhere(IQueryable source, Type elementType, BasePredicate predicate)
        {
            var method     = TypeFns.GetMethodByExample((IQueryable <String> q) => q.Where(s => s != null), elementType);
            var lambdaExpr = predicate.ToLambda(elementType);
            var func       = BuildIQueryableFunc(elementType, method, lambdaExpr);

            return(func(source));
        }
Пример #5
0
        public static IQueryable ApplyExpand(IQueryable source, Type elementType, string expand)
        {
            var method = TypeFns.GetMethodByExample((IQueryable <String> q) =>
                                                    EntityFrameworkQueryableExtensions.Include <String>(q, "dummyPath"), elementType);
            var func = QueryBuilder.BuildIQueryableFunc(elementType, method, expand);

            return(func(source));
        }
Пример #6
0
        public static IQueryable ApplyAsNoTracking(IQueryable source, Type elementType)
        {
            var method = TypeFns.GetMethodByExample((IQueryable <Object> q) =>
                                                    EntityFrameworkQueryableExtensions.AsNoTracking <Object>(q), elementType);
            var func = QueryBuilder.BuildIQueryableFunc(elementType, method);

            return(func(source));
        }
Пример #7
0
        public static IQueryable ApplySelect(IQueryable source, Type elementType, SelectClause selectClause)
        {
            var propSigs   = selectClause.Properties;
            var dti        = DynamicTypeInfo.FindOrCreate(propSigs.Select(ps => ps.Name), propSigs.Select(ps => ps.ReturnType));
            var lambdaExpr = CreateNewLambda(dti, propSigs);
            var method     = TypeFns.GetMethodByExample((IQueryable <String> q) => q.Select(s => s.Length), elementType, dti.DynamicType);
            var func       = BuildIQueryableFunc(elementType, method, lambdaExpr);

            return(func(source));
        }
Пример #8
0
        private IQueryable ApplyNestedOrderBy(IQueryable queryable, ODataQueryOptions queryOptions)
        {
            var    elementType       = TypeFns.GetElementType(queryable.GetType());
            var    result            = queryable;
            string inlinecountString = queryOptions.RawValues.InlineCount;

            if (!string.IsNullOrWhiteSpace(inlinecountString))
            {
                if (inlinecountString == "allpages")
                {
                    if (result is IQueryable)
                    {
                        var inlineCount = (Int64)Queryable.Count((dynamic)result);
                        queryOptions.Request.SetInlineCount(inlineCount);
                    }
                }
            }

            var orderByClauses = queryOptions.RawValues.OrderBy.Split(',').ToList();
            var isThenBy       = false;

            orderByClauses.ForEach(obc => {
                var func = QueryBuilder.BuildOrderByFunc(isThenBy, elementType, obc);
                result   = func(result);
                isThenBy = true;
            });

            var skipQueryString = queryOptions.RawValues.Skip;

            if (!string.IsNullOrWhiteSpace(skipQueryString))
            {
                var count  = int.Parse(skipQueryString);
                var method = TypeFns.GetMethodByExample((IQueryable <String> q) => Queryable.Skip <String>(q, 999), elementType);
                var func   = BuildIQueryableFunc(elementType, method, count);
                result = func(result);
            }

            var topQueryString = queryOptions.RawValues.Top;

            if (!string.IsNullOrWhiteSpace(topQueryString))
            {
                var count  = int.Parse(topQueryString);
                var method = TypeFns.GetMethodByExample((IQueryable <String> q) => Queryable.Take <String>(q, 999), elementType);
                var func   = BuildIQueryableFunc(elementType, method, count);
                result = func(result);
            }



            return(result);
        }
Пример #9
0
        public virtual IQueryable ApplyOrderBy(IQueryable queryable, ODataQueryOptions queryOptions)
        {
            var elementType = TypeFns.GetElementType(queryable.GetType());
            var result      = queryable;

            var orderByString = queryOptions.RawValues.OrderBy;

            if (!string.IsNullOrEmpty(orderByString))
            {
                var orderByClauses = orderByString.Split(',').ToList();
                var isThenBy       = false;
                orderByClauses.ForEach(obc => {
                    var parts        = obc.Trim().Replace("  ", " ").Split(' ');
                    var propertyPath = parts[0];
                    bool isDesc      = parts.Length > 1 && parts[1] == "desc";
                    var odi          = new OrderByClause.OrderByItem(parts[0], isDesc);

                    var func = QueryBuilder.BuildOrderByFunc(isThenBy, elementType, odi);
                    result   = func(result);
                    isThenBy = true;
                });
            }

            var skipQueryString = queryOptions.RawValues.Skip;

            if (!string.IsNullOrWhiteSpace(skipQueryString))
            {
                var count  = int.Parse(skipQueryString);
                var method = TypeFns.GetMethodByExample((IQueryable <String> q) => Queryable.Skip <String>(q, 999), elementType);
                var func   = BuildIQueryableFunc(elementType, method, count);
                result = func(result);
            }

            var topQueryString = queryOptions.RawValues.Top;

            if (!string.IsNullOrWhiteSpace(topQueryString))
            {
                var count  = int.Parse(topQueryString);
                var method = TypeFns.GetMethodByExample((IQueryable <String> q) => Queryable.Take <String>(q, 999), elementType);
                var func   = BuildIQueryableFunc(elementType, method, count);
                result = func(result);
            }

            return(result);
        }
Пример #10
0
        public virtual IQueryable ApplyOrderBy(IQueryable queryable, ODataQueryOptions queryOptions)
        {
            var elementType = TypeFns.GetElementType(queryable.GetType());
            var result      = queryable;

            var orderByString = queryOptions.RawValues.OrderBy;

            if (!string.IsNullOrEmpty(orderByString))
            {
                // apply the new order
                var orderByClauses = orderByString.Split(',').ToList();
                var isThenBy       = false;
                orderByClauses.ForEach(obc =>
                {
                    var func = QueryBuilder.BuildOrderByFunc(isThenBy, elementType, obc);
                    result   = func(result);
                    isThenBy = true;
                });
            }

            var skipQueryString = queryOptions.RawValues.Skip;

            if (!string.IsNullOrWhiteSpace(skipQueryString))
            {
                var count  = int.Parse(skipQueryString);
                var method = TypeFns.GetMethodByExample((IQueryable <String> q) => Queryable.Skip <String>(q, 999), elementType);
                var func   = BuildIQueryableFunc(elementType, method, count);
                result = func(result);
            }

            var topQueryString = queryOptions.RawValues.Top;

            if (!string.IsNullOrWhiteSpace(topQueryString))
            {
                var count  = int.Parse(topQueryString);
                var method = TypeFns.GetMethodByExample((IQueryable <String> q) => Queryable.Take <String>(q, 999), elementType);
                var func   = BuildIQueryableFunc(elementType, method, count);
                result = func(result);
            }

            return(result);
        }
Пример #11
0
        public override Expression ToExpression(ParameterExpression paramExpr)
        {
            var        navExpr     = NavPropBlock.ToExpression(paramExpr);
            var        elementType = NavPropBlock.Property.ElementType;
            MethodInfo mi;

            if (Operator == Operator.Any)
            {
                mi = TypeFns.GetMethodByExample((IQueryable <string> list) => list.Any(x => x != null), elementType);
            }
            else
            {
                mi = TypeFns.GetMethodByExample((IQueryable <string> list) => list.All(x => x != null), elementType);
            }

            var lambdaExpr  = Predicate.ToLambda(elementType);
            var castType    = typeof(IQueryable <>).MakeGenericType(new Type[] { elementType });
            var castNavExpr = Expression.Convert(navExpr, castType);
            var result      = Expression.Call(mi, castNavExpr, lambdaExpr);

            return(result);
        }
Пример #12
0
        private Expression BuildBinaryExpr(Expression expr1, Expression expr2, Operator op)
        {
            if (expr1.Type != expr2.Type)
            {
                if (TypeFns.IsNullableType(expr1.Type) && !TypeFns.IsNullableType(expr2.Type))
                {
                    expr2 = Expression.Convert(expr2, expr1.Type);
                }
                else if (TypeFns.IsNullableType(expr2.Type) && !TypeFns.IsNullableType(expr1.Type))
                {
                    expr1 = Expression.Convert(expr1, expr2.Type);
                }

                if (HasNullValue(expr2) && CannotBeNull(expr1))
                {
                    expr1 = Expression.Convert(expr1, TypeFns.GetNullableType(expr1.Type));
                }
                else if (HasNullValue(expr1) && CannotBeNull(expr2))
                {
                    expr2 = Expression.Convert(expr2, TypeFns.GetNullableType(expr2.Type));
                }
            }

            if (op == BinaryOperator.Equals)
            {
                return(Expression.Equal(expr1, expr2));
            }
            else if (op == BinaryOperator.NotEquals)
            {
                return(Expression.NotEqual(expr1, expr2));
            }
            else if (op == BinaryOperator.GreaterThan)
            {
                return(Expression.GreaterThan(expr1, expr2));
            }
            else if (op == BinaryOperator.GreaterThanOrEqual)
            {
                return(Expression.GreaterThanOrEqual(expr1, expr2));
            }
            else if (op == BinaryOperator.LessThan)
            {
                return(Expression.LessThan(expr1, expr2));
            }
            else if (op == BinaryOperator.LessThanOrEqual)
            {
                return(Expression.LessThanOrEqual(expr1, expr2));
            }
            else if (op == BinaryOperator.StartsWith)
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.StartsWith("abc"));
                return(Expression.Call(expr1, mi, expr2));
            }
            else if (op == BinaryOperator.EndsWith)
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.EndsWith("abc"));
                return(Expression.Call(expr1, mi, expr2));
            }
            else if (op == BinaryOperator.Contains)
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.Contains("abc"));
                return(Expression.Call(expr1, mi, expr2));
            }
            else if (op == BinaryOperator.In)
            {
                // TODO: need to generalize this past just 'string'
                var mi = TypeFns.GetMethodByExample((List <String> list) => list.Contains("abc"), expr1.Type);
                return(Expression.Call(expr2, mi, expr1));
            }

            return(null);
        }
Пример #13
0
        public override Expression ToExpression(Expression inExpr)
        {
            var exprs = _exprs.Select(e => e.ToExpression(inExpr)).ToList();
            var expr  = exprs[0];

            // TODO: add the rest ...
            if (FnName == "toupper")
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.ToUpper());
                return(Expression.Call(expr, mi));
            }
            else if (FnName == "tolower")
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.ToLower());
                return(Expression.Call(expr, mi));
            }
            else if (FnName == "trim")
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.Trim());
                return(Expression.Call(expr, mi));
            }
            else if (FnName == "length")
            {
                return(GetPropertyExpression(expr, "Length", typeof(int)));
            }
            else if (FnName == "indexof")
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.IndexOf("xxx"));
                return(Expression.Call(exprs[0], mi, exprs[1]));
            }
            else if (FnName == "concat")
            {
                // TODO: check if this works...
                var mi = TypeFns.GetMethodByExample((String s) => String.Concat(s, "xxx"));
                return(Expression.Call(mi, exprs[0], exprs[1]));
            }
            else if (FnName == "substring")
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.Substring(1, 5));
                return(Expression.Call(exprs[0], mi, exprs.Skip(1)));
            }
            else if (FnName == "replace")
            {
                // TODO: check if this works...
                var mi = TypeFns.GetMethodByExample((String s) => s.Replace("aaa", "bbb"));
                return(Expression.Call(exprs[0], mi, exprs[1], exprs[2]));
            }
            else if (FnName == "year")
            {
                return(GetPropertyExpression(expr, "Year", typeof(int)));
            }
            else if (FnName == "month")
            {
                return(GetPropertyExpression(expr, "Month", typeof(int)));
            }
            else if (FnName == "day")
            {
                return(GetPropertyExpression(expr, "Day", typeof(int)));
            }
            else if (FnName == "hour")
            {
                return(GetPropertyExpression(expr, "Hour", typeof(int)));
            }
            else if (FnName == "minute")
            {
                return(GetPropertyExpression(expr, "Minute", typeof(int)));
            }
            else if (FnName == "second")
            {
                return(GetPropertyExpression(expr, "Second", typeof(int)));
            }
            else if (FnName == "round")
            {
                // TODO: confirm that this works - is using static method.
                var mi = TypeFns.GetMethodByExample((Double d) => Math.Round(d));
                return(Expression.Call(mi, expr));
            }
            else if (FnName == "ceiling")
            {
                var mi = TypeFns.GetMethodByExample((Double d) => Math.Ceiling(d));
                return(Expression.Call(mi, expr));
            }
            else if (FnName == "floor")
            {
                var mi = TypeFns.GetMethodByExample((Double d) => Math.Floor(d));
                return(Expression.Call(mi, expr));
            }
            else if (FnName == "startswith")
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.StartsWith("xxx"));
                return(Expression.Call(exprs[0], mi, exprs[1]));
            }
            else if (FnName == "endsWith")
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.EndsWith("xxx"));
                return(Expression.Call(exprs[0], mi, exprs[1]));
            }
            else if (FnName == "substringof")
            {
                var mi = TypeFns.GetMethodByExample((String s) => s.Contains("xxx"));
                return(Expression.Call(exprs[0], mi, exprs[1]));
            }
            else
            {
                throw new Exception("Unable to locate Fn: " + FnName);
            }
        }