Expression Tree's like operator

.net entity-framework expression-trees lambda linq

Question

To dynamically filter Linq queries using string values, I have a Linq extension function. For instance:query.WhereHelper("columName", ">", 1) . I was able to utilize a variety of filter operators, such as GreaterThan, NotEqual, etc., but not "Like." Expression doesn't exist. Similar to or expression begins with etc. What is the best way to add the Like operator to my expression tree? Below is 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);
1
6
10/13/2010 11:28:41 AM

Accepted Answer

You would employExpression.Call using thestring.StartsWith , string.Contains , string.EndsWith and etc. The task of translating it back to TSQL rests with the consuming code. Be aware that there are more assistance methods here for LINQ-to-SQL but not for EF.

7
10/13/2010 11:31:20 AM

Popular Answer

LIKE expression can be defined 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);


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow