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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With