Entity Framework Filter “Expression>”

前端 未结 3 1664
余生分开走
余生分开走 2020-11-28 11:02

I\'m trying to create a filter method for Entity framework List and understand better the Expression

I have a Test Function like this.

3条回答
  •  野性不改
    2020-11-28 11:04

    It's useful to understand the difference between Expression> and Func<>.

    An Expression e => e.ID < 500 stores the info about that expression: that there's a T e, that you're accessing the property ID, calling the < operator with the int value 500. When EF looks at that, it might turn it into something like [SomeTable].[ID] < 500.

    A Func e => e.ID < 500 is a method equivalent to:

    static bool MyMethod(T e) { return e.ID < 500; }
    

    It is compiled as IL code that does this; it's not designed to be 'reconstituted' into a SQL query or anything else, only run.

    When EF takes your Expression, it must understand every piece of it, because it uses that to build a SQL query. It is programmed to know what the existing Where method means. It does not know what your Filter method means, even though it's a trivial method, so it just gives up.

提交回复
热议问题