score:4

Accepted answer

that's because inside your expression you are accessing a field. the exception tells you that you are accessing a field.

the expression is not evaluated when you create the query. it is only executed once you execute it. at that point, it will need to resolve the field. a workaround is getting the expression first into a local variable:

private static string getsomething(iqueryable<enumtest> things)
{
    var expression = program.converttostring;

    var ret = things
        .asexpandable()
        .select(c => expression.invoke(c.someenum))
        .first();
    return ret;
}

seeing that you are using this with entityframework, what will happen is that your expression will be converted to a sql query. however, since you are accessing a class inside the expression, it cannot convert this to a sql statement (how would it do that?). when you have an instance of the expression (with the local variable) you are eliminating this class access and that expression can be converted into sql.


Related Query

More Query from same tag