Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Like operator in Expression Tree

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);
like image 496
dstr Avatar asked Oct 13 '10 11:10

dstr


People also ask

What are the properties of expression trees?

One of the important properties of expression trees is that they are immutable, meaning in order to modify an existing expression tree, a new expression tree needs to be constructed by copying and modifying the existing tree expression.

How do you use expression trees?

When you want to have a richer interaction, you need to use Expression Trees. Expression Trees represent code as a structure that you can examine, modify, or execute. These tools give you the power to manipulate code during run time. You can write code that examines running algorithms, or injects new capabilities.

What is expression tree in C++?

An expression tree is basically a binary tree which is used to represent expressions. In an expression tree, internal nodes correspond to operators and each leaf nodes correspond to operands. Here is a C++ program to construct an expression tree for a prefix Expression in inorder, preorder and postorder traversals.

What is expression tree explain with example?

Expression Tree is a special kind of binary tree with the following properties: Each leaf is an operand. Examples: a, b, c, 6, 100. The root and internal nodes are operators. Examples: +, -, *, /, ^


1 Answers

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.

like image 103
Marc Gravell Avatar answered Sep 19 '22 00:09

Marc Gravell