Why am I getting an InvalidCastException when usin

2019-04-10 11:15发布

问题:

I'm just starting to use LinqKit with EntityFramework 6.0.2 and I have the following question...

Why does this:

public static readonly Expression<Func<MyEnum, string>> ConvertToString = e => 
        e == MyEnum.One
                    ? "one"
                    : e == MyEnum.Two
                        ? "two"
                        : "zero";

private static string GetSomethingElse(IQueryable<EnumTest> things)
{           
    var ret = things
        .AsExpandable()
        .Select(c => Program.ConvertToString.Invoke(c.SomeEnum))
        .First();
    return ret;
}

throw:

An unhandled exception of type 'System.InvalidCastException' 
    occurred in LinqKit.dll

Additional information: Unable to cast object of type     
    'System.Linq.Expressions.FieldExpression' to type 
    'System.Linq.Expressions.LambdaExpression'.

but this:

private static string GetSomething(IQueryable<EnumTest> things)
{
    Expression<Func<MyEnum, string>> ConvertToString = e => e == MyEnum.One
        ? "one"
        : e == MyEnum.Two
            ? "two"
            : "zero";

    var ret = things
        .AsExpandable()
        .Select(c => ConvertToString.Invoke(c.SomeEnum))
        .First();
    return ret;
}

works fine?

回答1:

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.