开发者

Why can't you return a List from a Compiled Query?

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 开发者_StackOverflow中文版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.


At a guess, this may be because the compiled query that returns an IQueryable can be lazy-loaded, whereas the compiled query that returns a List forces the query to be evaluated when the class is loaded (which is when Shared members are evaluated). Quite probably, at class load time, your database connection isn't set up, so evaluation of the query fails.

Try changing the declaration of CompiledSelect to a Shared property, and put a breakpoint in it to see when it is actually evaluated in each case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜