表达式树中的 Like 运算符
我有一个 Linq 扩展方法来使用字符串值动态过滤 Linq 查询。例如:query.WhereHelper("columName", ">", 1)
。我可以使用许多不同的过滤器运算符,例如 GreaterThan 或 NotEqual 等,但不能使用“Like”。没有 Expression.Like 或 Expression.StartsWith 等。如何在表达式树中实现 Like 运算符?这是我的代码:
public static IQueryable<T> WhereHelper<T>(this IQueryable<T> source, string columnName, object value, string filterType)
{
ParameterExpression table = Expression.Parameter(typeof(T), "");
Expression column = Expression.PropertyOrField(table, columnName);
Expression valueExpression = Expression.Convert(Expression.Constant(value), column.Type);
Expression where = null;
switch (filterType)
{
case "<":
where = Expression.LessThan(column, valueExpression);
break;
case "<=":
where = Expression.LessThanOrEqual(column, valueExpression);
break;
case "=":
where = Expression.Equal(column, valueExpression);
break;
case ">":
where = Expression.GreaterThan(column, valueExpression;
break;
case ">=":
where = Expression.GreaterThanOrEqual(column, valueExpression);
break;
case "<>":
where = Expression.NotEqual(column, valueExpression);
break;
}
Expression lambda = Expression.Lambda(where, new ParameterExpression[] { table });
Type[] exprArgTypes = { source.ElementType };
MethodCallExpression methodCall = Expression.Call(typeof(Queryable),
"Where",
exprArgTypes,
source.Expression,
lambda);
return (IQueryable<T>)source.Provider.CreateQuery<T>(methodCall);
I have a Linq extension method to dynamically filter Linq queries using string values. For example: query.WhereHelper("columName", ">", 1)
. I could use many different filter operators like GreaterThan or NotEqual etc. but not "Like". There is no Expression.Like or Expression.StartsWith etc. How can I implement Like operator to my Expression tree? Here's my code:
public static IQueryable<T> WhereHelper<T>(this IQueryable<T> source, string columnName, object value, string filterType)
{
ParameterExpression table = Expression.Parameter(typeof(T), "");
Expression column = Expression.PropertyOrField(table, columnName);
Expression valueExpression = Expression.Convert(Expression.Constant(value), column.Type);
Expression where = null;
switch (filterType)
{
case "<":
where = Expression.LessThan(column, valueExpression);
break;
case "<=":
where = Expression.LessThanOrEqual(column, valueExpression);
break;
case "=":
where = Expression.Equal(column, valueExpression);
break;
case ">":
where = Expression.GreaterThan(column, valueExpression;
break;
case ">=":
where = Expression.GreaterThanOrEqual(column, valueExpression);
break;
case "<>":
where = Expression.NotEqual(column, valueExpression);
break;
}
Expression lambda = Expression.Lambda(where, new ParameterExpression[] { table });
Type[] exprArgTypes = { source.ElementType };
MethodCallExpression methodCall = Expression.Call(typeof(Queryable),
"Where",
exprArgTypes,
source.Expression,
lambda);
return (IQueryable<T>)source.Provider.CreateQuery<T>(methodCall);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
Expression.Call
与string.StartsWith
、string.Contains
、string.EndsWith
等方法一起使用。它是供使用代码将其转换回 TSQL 的。请注意,对于 LINQ-to-SQL,这里还有一些额外的帮助器函数,但 EF 没有。You would use
Expression.Call
with thestring.StartsWith
,string.Contains
,string.EndsWith
etc methods. It is for the consuming code to translate it back to TSQL. Note that for LINQ-to-SQL there are also some additional helper functions here, but not with EF.您可以按如下方式定义 LIKE 表达式:
You can define LIKE expression as follows,