views:

207

answers:

4

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);
+3  A: 

On the line 104, when doing ToList conversion.

alex
There is no statement at line 104. Try setting a breakpoint there...
Guffa
+3  A: 

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.

Alan
There is no statement at line 104. Try setting a breakpoint there...
Guffa
You're right, but you are splitting hairs. The ToList is on line 104, not 102. This isn't a trick question, and he wanted to know if compiling the query means that it immediately executes, not whch of the 3 lines is a statement.
Alan
I'd like to think that it executes at 102/104 but it when I run a debugger and SQL Profiler, I see an SQL statement run after 101 executes and then 12 SQL Statements (one for each result on 101) run at 102/104
Antilogic
@Antilogic: It dpeends on how you've written your compiled query, right? If it can't be deferred, then it will execute immediately.
Alan
@Alan, see the original post I've updated it to contain an example of my compiled query.
Antilogic
@Antilogic You can see this behavior when executing statements one-by-one in debugger. Run through both statements without breakpoint and see how many SQL statements were issued.
alex
By the way, I hope you know that compiled query is not composable, so you first get all records into memory and then select those with ID > 5.
alex
+1  A: 

This is called Deferred Execution.
You can read a good post on it here.

Sani Huttunen
Yeah that's what I thought, and how I designed my application. However when the app executes line 101, I see a SQL statement fly by in SQL Profiler, and then another SQL Statement for every result returned by line 101.
Antilogic
A: 

On line 102. The lines 102-104 is a single statement, and the query is executed when the ToList method reads from the IEnumerable that the LINQ statement creates.

Guffa
Why the downvote? If you don't explain why, it's really pointless.
Guffa
Yeah. His answer is correct. +1 (not like it matters ;p)
Alan