0

I'm working on transforming some existing Linq to SQL into Compiled queries, in part using this helpful article as a guide.

Below is an example of one of my original statements:

 private IQueryable<Widget> GetWidgetQuery() { return db.Widgets.Where(u => (!u.SomeField.HasValue || !u.SomeField.Value)); } 

Here's my attempt at creating a compiled query:

 private static readonly Func<DBDataContext, IQueryable<Widget>> GetWidgetQuery = CompiledQuery.Compile((DBDataContext db) => db.Widgets.Where(u => (!u.SomeField.HasValue || !u.SomeField.Value))); 

I'm having some trouble visualizing the differences between the standard and compiled incarnations of this query. Assuming my syntax is proper, will the compiled query return the same data as the standard one, just with the advantages using Compiled queries provides?

2 Answers 2

1

Yes it will return the same data - the IQueryable<Widget> object - but unlike the first example, you'll lose the benefits of the compiled query if you extend the query further.

You will need to pass the DBDataContext object when you call GetWidgetQuery().

DBDataContext db; 

Returns IQueryable<Widget>:

var widgets = GetWidgetQuery(db); 

With LINQ to SQL, this loses the benefit of the compiled query by performing a LINQ query on the results:

var widgetsUncompiled = GetWidgetQuery(db).Where(u => u.SomeField.HasValue); 
Sign up to request clarification or add additional context in comments.

Comments

1

There is a difference between compiled and non-compiled queries in LINQ-to-SQL. Compiled queries execute immediately even if they are just returing IQueryable. Check out my question regarding this, might be related LINQ to SQL *compiled* queries and when they execute

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.