Can't return a List from a Compiled Query.
- by Andrew
I was speeding up my app by using compiled queries for queries which were getting hit over and over.
I tried to implement it like this:
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())
This did not work and I got this error message:
Type : System.ArgumentNullException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Value cannot be null.
Parameter name: value
However, when I changed my compiled query to return IQueryable instead of List like so:
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)
It worked fine. Can anyone shed any light as to why this is?
BTW, compiled queries rock! They sped up my app by a factor of 2.