Пример #1
0
        Expression ICustomFunctionOperatorConvertibleToExpression.Convert(ICriteriaToExpressionConverter converter, params Expression[] operands)
        {
            ConstantExpression typeNameExpr     = operands[1] as ConstantExpression;
            ConstantExpression propertyNameExpr = operands[2] as ConstantExpression;

            if (typeNameExpr == null)
            {
                throw new ArgumentException("The Upcast function expects a constant as the second argument (type name).");
            }
            if (propertyNameExpr == null)
            {
                throw new ArgumentException("The Upcast function expects a constant as the third argument (property name).");
            }
            string typeName     = typeNameExpr.Value as string;
            string propertyName = propertyNameExpr.Value as string;

            if (string.IsNullOrWhiteSpace(typeName))
            {
                throw new ArgumentException("The type name parameter is not a string value or an empty string");
            }
            if (string.IsNullOrWhiteSpace(propertyName))
            {
                throw new ArgumentException("The property name parameter is not a string value or an empty string");
            }
            Type type = GetTypeByName(typeName);

            return(Expression.Property(
                       Expression.Convert(operands[0], type),
                       propertyName
                       ));
        }
        Expression ICustomAggregateConvertibleToExpression.Convert(ICriteriaToExpressionConverter converter, Expression collectionProperty, ParameterExpression elementParameter, params Expression[] operands)
        {
            Type callFrom;

            if (typeof(ParallelQuery).IsAssignableFrom(collectionProperty.Type))
            {
                callFrom = typeof(ParallelEnumerable);
            }
            else
            {
                callFrom = typeof(Enumerable);
            }
            Expression operand       = Expression.Convert(operands[0], typeof(double));
            Expression operandPower2 = Expression.Call(typeof(Math), "Pow", new Type[] { }, operand, Expression.Constant(2.0));
            Expression sumOfSquares  = Expression.Call(callFrom, "Sum", new Type[] { elementParameter.Type }, collectionProperty, Expression.Lambda(operandPower2, elementParameter));
            Expression sum           = Expression.Call(callFrom, "Sum", new Type[] { elementParameter.Type }, collectionProperty, Expression.Lambda(operand, elementParameter));
            Expression count         = Expression.Call(callFrom, "Count", new Type[] { elementParameter.Type }, collectionProperty);

            count = Expression.Convert(count, typeof(double));
            Expression sumOfSquaresDivide2  = Expression.Divide(sumOfSquares, count);
            Expression sumDivideCountPower2 = Expression.Call(typeof(Math), "Pow", new Type[] { }, Expression.Divide(sum, count), Expression.Constant(2.0));
            Expression subtract             = Expression.Subtract(sumOfSquaresDivide2, sumDivideCountPower2);

            return(Expression.Call(typeof(Math), "Sqrt", new Type[] { }, subtract));
        }
Пример #3
0
 Expression ICustomFunctionOperatorConvertibleToExpression.Convert(ICriteriaToExpressionConverter converter, params Expression[] operands)
 {
     if (operands.Length != 2)
     {
         throw new ArgumentException("operands.Length != 2");
     }
     if (converter is CriteriaToEFExpressionConverter || converter is CriteriaToEFExpressionConverterInternal)
     {
         return(LikeFunction.MakeLinq(converter, operands[0], operands[1]));
     }
     return(((ICustomFunctionOperatorConvertibleToExpression)LikeFunction.Original).Convert(converter, operands));
 }
        Expression ICustomAggregateConvertibleToExpression.Convert(ICriteriaToExpressionConverter converter, Expression collectionProperty, ParameterExpression elementParameter, params Expression[] operands)
        {
            Type callFrom;

            if (typeof(ParallelQuery).IsAssignableFrom(collectionProperty.Type))
            {
                callFrom = typeof(ParallelEnumerable);
            }
            else
            {
                callFrom = typeof(Enumerable);
            }
            var lambda       = Expression.Lambda(operands[0], elementParameter);
            var selectCall   = Expression.Call(callFrom, "Select", new Type[] { elementParameter.Type, operands[0].Type }, collectionProperty, lambda);
            var distinctCall = Expression.Call(callFrom, "Distinct", new Type[] { operands[0].Type }, selectCall);

            return(Expression.Call(callFrom, "Count", new Type[] { operands[0].Type }, distinctCall));
        }
		Expression ICustomFunctionOperatorConvertibleToExpression.Convert(ICriteriaToExpressionConverter converter, Expression[] operands)
		{
			if (operands.Length != 2)
			{
				throw new ArgumentException("operands.Length != 2");
			}

			if (operands[0].NodeType == ExpressionType.Constant)
			{
				string str = ((ConstantExpression)operands[0]).Value?.ToString();
				if (str == null)
				{
					return Expression.Constant(false);
				}			

				var like = typeof(niterModel.Store.Extension).GetMethod("LikeFnc", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
				return Expression.Call(null, like, new Expression[] { EvalHelpers.SafeToString(operands[1]), Expression.Constant(str) });
			}
			return Expression.Call(EvalHelpers.SafeToString(operands[0]), "Contains", (Type[])null, new Expression[] { EvalHelpers.SafeToString(operands[1]) });
		}
        public Expression Convert(CriteriaOperatorCollection operands, ParameterExpression thisExpression, ICriteriaToExpressionConverter converter)
        {
            Type elementType = GetElementType(thisExpression);
            ParameterExpression elementParameter = Expression.Parameter(elementType, "elem");
            LambdaExpression    groupSelect      = Expression.Lambda(converter.Convert(elementParameter, operands[1]), elementParameter);
            Expression          groupEpression   = Expression.Call(typeof(Enumerable), "GroupBy", new Type[] { elementType, groupSelect.Body.Type }, thisExpression, groupSelect);

            return(Expression.Call(typeof(Enumerable), "Count", new Type[] { groupEpression.Type.GetGenericArguments()[0] }, groupEpression));
        }
Пример #7
0
        static Expression MakeLinq(ICriteriaToExpressionConverter converter, Expression vExpr, Expression pExpr)
        {
            if (pExpr.NodeType == ExpressionType.Constant)
            {
                object boxedVal = ((ConstantExpression)pExpr).Value;
                string pattern  = boxedVal != null?boxedVal.ToString() : null;

                if (pattern == null)
                {
                    return(Expression.Constant(false));
                }
                string body        = pattern;
                bool   lastPercent = body.EndsWith("%");
                if (lastPercent)
                {
                    body = body.Substring(0, body.Length - 1);
                }
                bool firstPercent = body.StartsWith("%");
                if (firstPercent)
                {
                    body = body.Substring(1);
                }
                if (!body.Contains('%') && !body.Contains('_') && !body.Contains('[') && (lastPercent || firstPercent))
                {
                    var stringExpr = EvalHelpers.SafeToString(vExpr);
                    {
                        var maybeIifConverter = converter as CriteriaToExpressionConverterInternal;
                        if (maybeIifConverter != null && maybeIifConverter.ForceIifForInstance)
                        {
                            Expression <Func <string, bool> > method;
                            if (!firstPercent)
                            {
                                method = s => s != null?s.StartsWith(body) : false;
                            }
                            else if (!lastPercent)
                            {
                                method = s => s != null?s.EndsWith(body) : false;
                            }
                            else
                            {
                                method = s => s != null?s.Contains(body) : false;
                            }
                            return(Expression.Invoke(Expression.Constant(method), stringExpr));
                        }
                    }
                    string methodName;
                    if (!firstPercent)
                    {
                        methodName = "StartsWith";
                    }
                    else if (!lastPercent)
                    {
                        methodName = "EndsWith";
                    }
                    else
                    {
                        methodName = "Contains";
                    }
                    return(Expression.Call(stringExpr, methodName, null, Expression.Constant(body)));
                }
            }
            return(((ICustomFunctionOperatorConvertibleToExpression)LikeFunction.Original).Convert(converter, vExpr, pExpr));
        }
Пример #8
0
        Expression ICustomFunctionOperatorConvertibleToExpression.Convert(ICriteriaToExpressionConverter converter, params Expression[] operands)
        {
            object currentUserDepartment = ((ICustomPermissionPolicyUser)SecuritySystem.CurrentUser).Department;

            return(Expression.Constant(currentUserDepartment));
        }