表达式树中的 Like 运算符

发布于 2024-09-27 17:36:29 字数 1914 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃兔兔 2024-10-04 17:36:29

您可以将Expression.Callstring.StartsWithstring.Containsstring.EndsWith等方法一起使用。它是供使用代码将其转换回 TSQL 的。请注意,对于 LINQ-to-SQL,这里还有一些额外的帮助器函数,但 EF 没有。

You would use Expression.Call with the string.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.

太阳哥哥 2024-10-04 17:36:29

您可以按如下方式定义 LIKE 表达式:

var propertyName = "Firstname";
var propertyValue= "xxxx";

MethodInfo refmethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var parameter = Expression.Parameter(typeof(T), "type");
var property = Expression.Property(parameter, propertyName);
var value = Expression.Constant(propertyValue, typeof(string));
var containsMethodExp = Expression.Call(property, refmethod, value);

You can define LIKE expression as follows,

var propertyName = "Firstname";
var propertyValue= "xxxx";

MethodInfo refmethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var parameter = Expression.Parameter(typeof(T), "type");
var property = Expression.Property(parameter, propertyName);
var value = Expression.Constant(propertyValue, typeof(string));
var containsMethodExp = Expression.Call(property, refmethod, value);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文