开发者

Working with nullable types in Expression Trees

开发者 https://www.devze.com 2023-01-18 18:42 出处:网络
I have an extension method to dynamically filter Linq to Entities results using string values. It works fine until I use it to filter nullable columns. Here\'s my code:

I have an extension method to dynamically filter Linq to Entities results using string values. It works fine until I use it to filter nullable columns. Here's my code:

public static IOrderedQueryable<T> OrderingHelperWhere<T>(this IQueryable<T> source, string columnName, object value)
{
    ParameterExpression table = Expression.Parameter(typeof(T), "");
    Expression column = Expression.PropertyOrField(table, columnName);
    Expression where = Expression.GreaterThanOrEqual(column, Expression.Constant(value));
    Expression lambda = Expression.Lambda(where, new ParameterExpression[] { table });

    Type[] exprArgTypes = { source.ElementType };

    MethodCallExpression 开发者_Python百科methodCall = Expression.Call(typeof(Queryable), 
                                                      "Where", 
                                                      exprArgTypes, 
                                                      source.Expression, 
                                                      lambda);

    return (IOrderedQueryable<T>)source.Provider.CreateQuery<T>(methodCall);
}

Here's how I use it:

var results = (from row in ctx.MyTable select row)
              .OrderingHelperWhere("userId", 5);//userId is nullable column

Here's the exception I'm getting when I use this for nullable table columns:

The binary operator GreaterThanOrEqual is not defined for the types 'System.Nullable`1[System.Int32]' and 'System.Int32'

I couldn't figured this out. What should I do?


I had to convert the value type to the column type using Expression.Convert:

Expression where = Expression.GreaterThanOrEqual(column, Expression.Convert(Expression.Constant(value), column.Type));


A type can be passed to the Expression.Constant function as a second argument. Something like typeof(int?) or, in the case of this question, column.Type.

e.g.

Expression.Constant(value, column.Type)


You can check if a type is nullable by doing: if (typeof(T).Equals(typeof(Nullable<>)) I believe and then proceed to handle that specially. If you can invoke the GetValueOrDefault() method somehow, that would work, or programmbly create the comparison value to be of the same type maybe.

HTH.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号