在表达式树中使用可为 null 的类型
我有一个扩展方法可以使用字符串值动态过滤 Linq to Entities 结果。它工作正常,直到我使用它来过滤可为空的列。这是我的代码:
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 methodCall = Expression.Call(typeof(Queryable),
"Where",
exprArgTypes,
source.Expression,
lambda);
return (IOrderedQueryable<T>)source.Provider.CreateQuery<T>(methodCall);
}
这是我如何使用它:
var results = (from row in ctx.MyTable select row)
.OrderingHelperWhere("userId", 5);//userId is nullable column
这是当我将其用于可为空的表列时遇到的异常:
没有为类型“System.Nullable`1[System.Int32]”和“System.Int32”定义二元运算符 GreaterThanOrEqual
我无法弄清楚这一点。我应该怎么办?
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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我必须使用 Expression.Convert 将值类型转换为列类型:
I had to convert the value type to the column type using Expression.Convert:
类型可以作为第二个参数传递给Expression.Constant函数。类似 typeof(int?) 的内容,或者在本问题中为 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.
您可以通过执行以下操作来检查类型是否可为空: if (typeof(T).Equals(typeof(Nullable<>)) 我相信,然后继续专门处理该问题。如果您可以以某种方式调用 GetValueOrDefault() 方法,那么可以,或者以编程方式创建相同类型的比较值
。
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.