【发布时间】:2010-03-09 06:19:43
【问题描述】:
我正在通过使用编译后的查询来加速我的应用程序,这些查询一次又一次地受到打击。
我尝试这样实现它:
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 倍。
【问题讨论】:
标签: vb.net linq-to-sql compiled-query