今天在找一個Performance issue時, 打開Sql profiler突然看到一段EF查詢直接做Full table scan, 追了一下原來是傳過去的條件型態宣告為: Func<T,bool> ; 注意Func在Linq或lambda expression中並無法直接轉為sql,必須改為傳遞Expression<Func<T>>。
If you want
your lambda to be executed in SQL, you need to pass it as an Expression, not a
Function.
Func<T>
denotes a delegate which is pretty much a pointer to a method and
Expression<Func<T>> denotes a tree data structure for a lambda
expression.
沒有留言:
張貼留言