开发者

LINQ to SQL and compiled queries

开发者 https://www.devze.com 2023-02-08 15:55 出处:网络
I have a compiled query that returns an IQueryable<tblMyTable>.If I execute t开发者_运维技巧he compiled query, are the results cached in the DataContext I pass?

I have a compiled query that returns an IQueryable<tblMyTable>. If I execute t开发者_运维技巧he compiled query, are the results cached in the DataContext I pass?

using(context)
{
    var count = MyCompiledQuery(context).Count();

    //Does the call to MyCompiledQuery execute against the database again, or does it go to the context for results?
    var first10 = MyCompiledQuery(context).Take(10);
}

This is a .NET 3.5 application using C#.


Yes, the query is executed again. You can see it by running SQL Profiler parallel to you app.


The only way you get this to work would be to get all of the records first and do a ToList() or ToArray(), and then Count() and Take(10) would run against the list or array. But I'm guessing you don't want to get all the results.

One optimization you could do here is to explicitly open and close the connection, but I've read that connection pooling is so efficient that you might not notice much difference.

This info might clarify things

0

精彩评论

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

关注公众号