我通过使用编译的查询来加速我的应用程序,这些查询一次又一次地受到打击。
我试图这样实现它:
Function Select(ByVal fk_id As Integer) As List(SomeEntity)
Using db As New DataContext()
db.ObjectTrackingEnabled = False
Return CompiledSelect(db, fk_id)
End Using
End Function
Shared CompiledSelect As Func(Of DataContext, Integer, List(Of SomeEntity)) = _
CompiledQuery.Compile(Function(db As DataContext, fk_id As Integer) _
(From u In db.SomeEntities _
Where u.SomeLinkedEntity.ID = fk_id _
Select u).ToList())
这不起作用,我收到以下错误消息:
Type : System.ArgumentNullException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Value cannot be null.
Parameter name: value
但是,当我将编译的查询更改为返回 IQueryable 而不是 List 时,如下所示:
Function Select(ByVal fk_id As Integer) As List(SomeEntity)
Using db As New DataContext()
db.ObjectTrackingEnabled = False
Return CompiledSelect(db, fk_id).ToList()
End Using
End Function
Shared CompiledSelect As Func(Of DataContext, Integer, IQueryable(Of SomeEntity)) = _
CompiledQuery.Compile(Function(db As DataContext, fk_id As Integer) _
From u In db.SomeEntities _
Where u.SomeLinkedEntity.ID = fk_id _
Select u)
它工作得很好。任何人都可以解释为什么会这样吗?
顺便说一句,编译查询摇滚!他们将我的应用程序加速了 2 倍。