Mike_G
Mike_G

Reputation: 16502

LINQ to SQL and compiled queries

I have a compiled query that returns an IQueryable<tblMyTable>. If I execute the 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#.

Upvotes: 2

Views: 400

Answers (2)

carlsb3rg
carlsb3rg

Reputation: 772

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

Upvotes: 0

Andrei
Andrei

Reputation: 4298

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

Upvotes: 2

Related Questions