Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does a compiled query that returns an IQueryable execute?

Okay I need a sanity check here...

I've compiled a query that returns an IQueryable when executed.

On what line(s) should the query actually execute against the database 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>();

Here is how I define the 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 REFER TO: LINQ to SQL compiled queries and when they execute

like image 226
Adam Ritenauer Avatar asked Sep 18 '09 15:09

Adam Ritenauer


2 Answers

It executes at line 104 (when you call ToList()).

A compiled query is a query that is translated only once to TSQL at compile time, instead of everytime prior to execution.

like image 73
Alan Avatar answered Oct 07 '22 19:10

Alan


This Query executes on line 101. I verified it by doing a SQL Profiler trace. I guess this is because it is a compiled query.

The filtering you are doing afterwords > 5 is done in memory.

like image 38
lahsrah Avatar answered Oct 07 '22 19:10

lahsrah