Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameterized Linq Expression Help

I want to do a method with a signature like this:

Expression<Func<TSource, bool>> CreatePropertyFilter<TSource>(Expression<Func<TSource, string>> selector, string value, TextMatchMode matchMode);

Basically, it takes a property selector (ex: p = p.Name), a string value and a enum value that can be StartsWith, EndsWith, Contains, Exact; for text matching options.

How can I implement the method in a way that LINQ2Entities can understand? I already implemented the method using nested invocation expressions like this:

Expression<Func<string, bool>> comparerExpression;

switch (matchMode)
{
    case TextMatchMode.StartsWith:
       comparerExpression = p => p.StartsWith(value);
       break;
    case TextMatchMode.EndsWith:
       comparerExpression = p => p.EndsWith(value);
       break;
    case TextMatchMode.Contains:
       comparerExpression = p => p.Contains(value);
       break;
    default:
       comparerExpression = p => p.Equals(value);
       break;
}

var equalityComparerParameter = Expression.Parameter(typeof(IncomingMail), null);
var equalityComparerExpression = Expression.Invoke(comparerExpression, Expression.Invoke(selector, equalityComparerParameter));
var equalityComparerPredicate = Expression.Lambda<Func<IncomingMail, bool>>(equalityComparerExpression, equalityComparerParameter);

The problem is that Linq2Entities doesn't support Invocation expressions.

Any advice on this?

Thanks!

like image 784
Luis Aguilar Avatar asked Mar 27 '11 05:03

Luis Aguilar


1 Answers

Essentially, given a selector:

input => input.Member

You are currently constructing a predicate expression like:

input => selector(input).Method(value)

Instead, 'expand' out the selector expression by using its body (a MemberExpression), to construct something like:

input => input.Member.Method(value) 

This would look like:

private static Expression<Func<TSource, bool>> CreatePropertyFilter<TSource>
    (Expression<Func<TSource, string>> selector, 
     string value, 
     TextMatchMode matchMode)
{
    // Argument-checking here.    

    var body = selector.Body as MemberExpression;

    if (body == null)
        throw new ArgumentException("Not a MemberExpression.");    

    // string.StartsWith / EndsWith etc. depending on the matchMode.
    var method = typeof(string)
                 .GetMethod(GetMethodName(matchMode), new[] { typeof(string) });

    // input.Member.method(value)
    var compEx = Expression.Call(body, method, Expression.Constant(value));

    // We can reuse the parameter of the source.
    return Expression.Lambda<Func<TSource, bool>>(compEx, selector.Parameters);
}

Where the translating method is:

// I really don't like this enum.
// Why not explicitly include Equals as a member?
private static string GetMethodName(TextMatchMode mode)
{
    switch (mode)
    {
        case TextMatchMode.StartsWith:
            return "StartsWith";

        case TextMatchMode.EndsWith:
                return "EndsWith";

        case TextMatchMode.Contains:
            return "Contains";

        default:
            return "Equals";
    }    
}
like image 74
Ani Avatar answered Sep 24 '22 01:09

Ani