Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Because Linq-to-Sql uses a <code>QueryProvider</code> that translates <code>Expression</code>s to SQL statements, it has to do so for every query executed by that provider (unless the query is precompiled [more on that further down]). So, for instance at a basic level:</p> <pre><code>var people = context.People.Where(p =&gt; p.Name == "Matt"); </code></pre> <p>Linq-to-Sql needs to convert the expression <code>p =&gt; p.Name == "Matt"</code> into an expression tree, which it in turn converts to a SQL statement, something akin to:</p> <pre><code>SELECT t0.Name FROM People t0 WHERE t0.Name = 'Matt' </code></pre> <p>The query when executed against Sql Server, which in turn needs to generate an execution plan for the query and run it against the table to get the results. It's pretty efficient at creating the right query for the right job, and supports more of an ad-hoc approach to data querying.</p> <p>With stored procedures, Linq-to-Sql doesn't have to generate an expression tree and from that generate the sql, instead the designer generated class has all in information it needs to pass the arguments from the method to the stored procedure. </p> <p>In that sense, its more efficient to use stored procedures, but you lose the ability to do these ad-hoc queries which are so often useful.</p> <p>You may find it really comes down to a preference of using sprocs vs. direct queries in your database (discuss this with your DBA).</p> <p>Compiled queries allow you the benefits of a pre-compiled statement (the query is only generated once), so subsequent queries will use the previously compiled query. An example would be:</p> <pre><code>public IQueryable&lt;Product&gt; GetProduct(int id) { // Normal query, expression tree and sql generated each time it is // it is executed against the data source. return context.Products.Where(p =&gt; p.Id == id); } </code></pre> <p>Whereas a compiled query:</p> <pre><code>private static readonly Func&lt;DataContext, int, IQueryable&lt;Product&gt;&gt; ProductById = CompiledQuery.Compile((context, id) =&gt; context.Products.Where(p =&gt; p.Id == id)); public IQueryable&lt;Product&gt; GetProduct(int id) { return ProductById(context, id); } </code></pre> <p>The latter will use the precompiled query, so it only generates the expression tree and sql once.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload