Linq To Sql: Compiled Queries and Extension Methods

I am wondering how Linq2Sql handles a compiled query that returns an IQueryable.

If I call an extension method based on a compiled request like GetEntitiesCompiled (). Count () "or" GetEntitiesCompiled (). Take (x) ". What is Linq2Sql doing in the background? That would be very bad, so in this situation I have to write a compiled query like" CountEntitiesCompiled ".

Does it load the result (in this case "GetEntitiesCompiled ()") into memory (mapped to an entity class like "ToList ()")?

So what situations make sense when compiled queries return IQueryable, this query cannot change, before querying Sql-Server. So in my opinion I can just like a good return list.

Thanks for answers!

+2


a source to share


2 answers


As I understand it - if it can't use the precompiled query exactly (because you compiled it further), it just runs it like any normal query IQueryable

, so it really will still output SELECT COUNT(1) FROM ...

(it doesn't have to iterate over the entire table / everything) ...

But the real answer: profile; you can hook .Log

to see TSQL like:



myDataContext.Log = Console.Out; // write TSQL to the console

      

or just use SQL trace to see what goes up and down the wire.

0


a source


Linq2Sql is not smart enough in such cases. In my experience, it always runs the compiled part as is. In case GetEntitiesCompiled().Count()

it will fetch all records and then execute in memory Count()

.



0


a source







All Articles