Accepted answer

i think you want something like:

public static expression<func<tresult, bool>> getpredicate<tkey>
    (expression<func<tresult, tkey>> selector, tresult input, object value)
    // note: move "early out" here so that bulk of method is less deeply nested.
    // really? why make this generic in tkey then?
    if (typeof(tkey) != typeof(string))
        throw new exception("type not supported");

    var parameter = expression.parameter("input");
    var invocation = expression.invoke(selector, input);
    var constant = expression.constant(value);
    var equality = expression.equal(invocation, constant);
    return expression.lambda<func<tresult, bool>>(equality, parameter);

i'm not quite sure what kind of equality that will use, mind you - it's entirely possible it's going to use a reference equality operation, whereas i suspect you want value equality. you'll have to try it to see, being careful in tests due to string interning.


return p => (selector.compile().invoke(input) as string) == value

Related Query

More Query from same tag