Erroneous comparisons in expression trees

I am trying to create a dynamic LINQ query in the Entity Framework from a set of set criteria provided by the user. In the end, this will include more complex behavior, but currently I have a list of field names and values, and I want to return all records in which field names have these values.

My basic structure is this:

public IEnumerable<ThingViewModel> getMythings(SelectionCriteria selectionCriteria)
{
    var predicate = constructPredicate<Thing>(selectionCriteria);
    var things = this.dbContext.Things.Where(predicate).ToList();
    return Mapper.Map<List<Thing>, List<ThingViewModel>>(things);
}

Where is all the interesting work in constructPredicate ():

private static Expression<Func<T, bool>> constructPredicate<T>(SelectionCriteria selectionCriteria)
{
    // using Pete Montgomery PredicateBuilder:
    // http://petemontgomery.wordpress.com/2011/02/10/a-universal-predicatebuilder/

    var predicate = PredicateBuilder.True<T>();

    foreach (var item in selectionCriteria.andList)
    {
        // Accessing foreach values in closures can result in unexpected results.
        // http://stackoverflow.com/questions/14907987/access-to-foreach-variable-in-closure
        var fieldName = item.fieldName;
        var fieldValue = item.fieldValue;

        var parameter = Expression.Parameter(typeof (T), "t");
        var property = Expression.Property(parameter, fieldName);
        var value = Expression.Constant(fieldValue);

        var lambda = buildCompareLambda<T>(property, value, parameter);

        predicate = predicate.And(lambda);
    }

    return predicate;
}

, buildCompareLambda() . , . , . , , NULL, .

buildCompareLambda(), :

private static Expression<Func<T, bool>> buildCompareLambda<T>(
    MemberExpression property,
    ConstantExpression value,
    ParameterExpression parameter)
{
    Expression<Func<T, bool>> lambda = null;
    if (property.Type == typeof (string))
        lambda = buildStringCompareLambda<T>(property, value, parameter);
    else if (property.Type.IsGenericType && Nullable.GetUnderlyingType(property.Type) != null)
        lambda = buildNullableCompareLambda<T>(property, value, parameter);

    if (lambda == null)
        throw new Exception(String.Format("SelectrionCriteria cannot handle property type '{0}'", property.Type.Name));

    return lambda;
}

, buildStringCompareLambda :

private static Expression<Func<T, bool>> buildStringCompareLambda<T>(
    MemberExpression property,
    ConstantExpression value,
    ParameterExpression parameter)
{
    var equalsMethod = typeof (string).GetMethod("Equals", 
            new[] {typeof (string), typeof (string)});
    var comparison = Expression.Call(equalsMethod, property, value);
    return Expression.Lambda<Func<T, bool>>(comparison, parameter);
}

buildNullableCompareLambda() :

private static Expression<Func<T, bool>> buildNullableCompareLambda<T>(MemberExpression property,
    ConstantExpression value,
    ParameterExpression parameter)
{
    var underlyingType = Nullable.GetUnderlyingType(property.Type);

    if (underlyingType == typeof (int) || underlyingType == typeof (Int16) || underlyingType == typeof (Int32) ||
        underlyingType == typeof (Int64) || underlyingType == typeof (UInt16) || underlyingType == typeof (UInt32) ||
        underlyingType == typeof (UInt64))
    {
        var equalsMethod = underlyingType.GetMethod("Equals", new[] {underlyingType});

        var left = Expression.Convert(property, underlyingType);
        var right = Expression.Convert(value, underlyingType);

        var comparison = Expression.Call(left, equalsMethod, new Expression[] {right});

        return Expression.Lambda<Func<T, bool>>(comparison, parameter);
    }

    return null;
}

buildNullableCompareLambda() , buildCompareLambda() buildNullableCompareLambda(). - ints. , , , , , EF Int16 Int32. , .

SO , , , , , , ' .

, , , , null, , . " , ".

SQL , WHERE NULL, null "WHERE field = 'value", . , ​​ .

?

: , Expression.Equal().

:

private static Expression<Func<T, bool>> constructPredicate<T>(SelectionCriteria selectionCriteria)
{
    var predicate = PredicateBuilderEx.True<T>();
    var foo = PredicateBuilder.True<T>();

    foreach (var item in selectionCriteria.andList)
    {
        var fieldName = item.fieldName;
        var fieldValue = item.fieldValue;

        var parameter = Expression.Parameter(typeof (T), "t");
        var property = Expression.Property(parameter, fieldName);
        var value = Expression.Constant(fieldValue);

        var comparison = Expression.Equal(property, value);
        var lambda = Expression.Lambda<Func<T, bool>>(comparison, parameter);

        predicate = PredicateBuilderEx.And(predicate, lambda);
    }
    return predicate;
}

. :

Equal 'System.Nullable`1 [System.Int16]' 'System.Int16'.

+4
2

, , , , , .

Expression.Equal , . , . :

private static Expression<Func<T, bool>> constructPredicate<T>(SelectionCriteria selectionCriteria)
{
    var predicate = PredicateBuilderEx.True<T>();
    var foo = PredicateBuilder.True<T>();

    foreach (var item in selectionCriteria.andList)
    {
        var fieldName = item.fieldName;
        var fieldValue = item.fieldValue;

        var parameter = Expression.Parameter(typeof (T), "t");
        var property = Expression.Property(parameter, fieldName);
        var value = Expression.Constant(fieldValue);
        var converted = Expression.Convert(value, property.Type);

        var comparison = Expression.Equal(property, converted);
        var lambda = Expression.Lambda<Func<T, bool>>(comparison, parameter);

        predicate = PredicateBuilderEx.And(predicate, lambda);
    }

    return predicate;
}

, .

+4

, buildNullableCompareLambda<T> . , Equals, , , . . .

:

var property = Expression.Property(parameter, fieldName);
var value = Expression.Constant(fieldValue);
var lambda = Expression.Equal(property, value);

Edit:

, . (). , , :

left.Type right.Type , node . node . left.Type right.Type , node . node - Boolean.

, , NULL, - . , , .

+1

Source: https://habr.com/ru/post/1542378/


All Articles