public List <Criterion> Extract()
        {
            var result = new List <Criterion>();

            foreach (var ex in GetUnitExpressions(Criteria.Body))
            {
                var condition = Criterion.From(ex);

                if (condition == null || (condition as BinaryCriterion)?.IsConvertedCompletely == false)
                {
                    if (ThrowOnNonConversion)
                    {
                        throw new Exception("Failed to extract a criterion from expression: " + ex);
                    }

                    NotConverted.Add(ex);
                }

                result.Add(condition);
            }

            return(result);
        }
Beispiel #2
0
 private static Criterion CreateCriterion(MethodCallExpression expression)
 {
     return(Criterion.From(expression, throwOnError: false));
 }
        internal static Criterion CreateCriterion(Expression expression)
        {
            var binary = expression as BinaryExpression;

            if (binary != null)
            {
                var member = binary.Left as MemberExpression;

                if (member != null)
                {
                    var property = GetPropertyExpression(member);

                    if (property.IsEmpty())
                    {
                        return(null);
                    }

                    var value = GetExpressionValue(binary.Right);
                    value = value?.ToString();
                    return(new Criterion(property, ToOperator(binary.NodeType), value));
                }
                else if (binary.Left is ParameterExpression)
                {
                    var param = binary.Left as ParameterExpression;
                    return(new Criterion("ID", ToOperator(binary.NodeType), GetExpressionValue(binary.Right)));
                }
                else
                {
                    return(null);
                }
            }

            var unary = expression as UnaryExpression;

            if (unary != null)
            {
                if (unary.NodeType != ExpressionType.Not)
                {
                    return(null);
                }

                var member = unary.Operand as MemberExpression;
                if (member == null)
                {
                    return(null);
                }

                var property = GetPropertyExpression(member);

                if (property.IsEmpty())
                {
                    return(null);
                }

                return(new Criterion(property, FilterFunction.Is, false));
            }

            // Boolean property:
            if (expression is MemberExpression && expression.NodeType == ExpressionType.MemberAccess)
            {
                var member = expression as MemberExpression;

                var property = GetPropertyExpression(member);

                if (property.IsEmpty())
                {
                    return(null);
                }
                if (property == "HasValue")
                {
                    property = (member.Expression as MemberExpression)?.Member?.Name;
                    if (property.IsEmpty())
                    {
                        return(null);
                    }
                    return(new Criterion(property, FilterFunction.IsNot, value: null));
                }

                if (((member as MemberExpression).Member as PropertyInfo).PropertyType != typeof(bool))
                {
                    return(null);
                }

                // Only one property level is supported:
                return(new Criterion(property, FilterFunction.Is, true));
            }

            // Method call
            var methodCall = expression as MethodCallExpression;

            if (methodCall != null)
            {
                return(Criterion.From(methodCall, throwOnError: false));
            }

            return(null);
        }