When is a compiled query executed that returns IQueryable execution?

Ok, I need a sanity check here ...

I compiled a query that returns IQueryable when it is executed.

On which line (s) should the query against the database be executed in the following example?

101 IQueryable<T> results = MyCompiledQuery(MyDataContext); 102 List<T> final = (from t in result 103 where t.ID > 5 104 select t).ToList<T>(); 

This is how I define a compiled query

  public static Func<MyDataContext, IQueryable<Widget>> MyCompiledQuery= CompiledQuery.Compile<MyDataContext, IQueryable<Widget>>( (MyDataContext db) => from w in db.Widgets where ((w.Type == WidgetType.Atype || //Widget.Atype is a Linq to Sql object, that I've defined statically w.Type == WidgetType.Btype || //See above comment w.Type == WidgetType.Ctype ) && //See above comment w.Location == WidgetLocation.Domestic) //Samething applies here select euc); 

FOR ADDITIONAL DISCUSSION PLEASE REQUEST: LINQ to SQL compiled queries and when they are executed

+6
c # linq-to-sql
source share
5 answers

"On line 104, when executing a ToList conversion."

Well, this answer is incorrect. We call the delegate stored in the MyCompiledQuery variable on line 101, which returns the result of the compiled query, not the query itself.

+2
source share

Executed on line 104 (when you call ToList ()).

A compiled query is a query that is translated only once in TSQL at compile time, and not every time before execution.

+2
source share

This query is executed on line 101. I checked it by doing an SQL Profiler trace. I think this is because it is a compiled request.

The filtering you do after word> 5 is performed in memory.

+1
source share

This is called deferred execution.
You can read a good post on it here .

0
source share

As far as I know, IQueryable never starts, it just converts the Linq query to the requested format so that it runs whenever it is requested.

In this case, I assume that at the moment when he was asked to convert to a List, he asks for the result. And it makes no sense to fight with lines 102 and 104, since both are one line.

0
source share

All Articles