Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try this one please: <br /></p> <pre><code>ctx.Orders.Include("OrderItems").Where(c => c.OrderID == id).FirstOrDefault(); </code></pre> <p><br /> By the way you don't need to look into SQL Profiler to see the generated SQL, you can do it right inside your code by writing: <br /></p> <pre><code>IQueryable&lt;Order> query = ctx.Orders.Include("OrderItems") .Where(c => c.OrderID == id); string sql = ((ObjectQuery&lt;Order>)query).ToTraceString(); </code></pre> <p><br /> <strong>EDIT:</strong> <br /> Question: What if we have a function like <em>FindOrders</em> and we need to pass the predicate to this function? Answer: The code should looks like: <br /></p> <pre><code>public List&lt;Order> FindOrders(Expression&lt;Func&lt;Order, bool>> predicate) { using (DBContext ctx = new DBContext()) { return ctx.Orders.Include("OrderItems").Where(predicate).ToList&lt;Order>(); } } //Calling the function: var order = FindOrders(c => c.OrderID == id)[0]; </code></pre> <p><br /> This time, if you check your SQL Profiler you'll see there is a where clause in the SQL that's been submitted to SQL Server. <br /><br /> <strong>Explanation:</strong> <br /> The reason for this "Weird behavior" is that basically when you write Where(c => c.OrderID == id), C# compiler cast your lambda expression into an Expression&lt;Func&lt;TSource, int, bool>> and NOT to a Func&lt;TSource, int, bool>. <br /><br /> <a href="http://msdn.microsoft.com/en-us/library/bb548547.aspx" rel="noreferrer"><b>MSDN Documentation for Queryable.Where</b></a> also confirms this: <code><pre> public static IQueryable&lt;TSource> Where&lt;TSource>( this IQueryable&lt;TSource> source, Expression&lt;Func&lt;TSource, int, bool>> predicate ) </code></pre> <br /> However if you explicitly pass a Func&lt;TSource, int, bool>> to the Where method then you are basically calling <a href="http://msdn.microsoft.com/en-us/library/bb549418.aspx" rel="noreferrer"><b>Enumerable.Where:</b></a> <br /><br /> <code><pre> public static IEnumerable&lt;TSource> Where&lt;TSource>( this IEnumerable&lt;TSource> source, Func&lt;TSource, int, bool> predicate ) </code></pre> <br /> And as we know IEnumerable.Where is "LINQ to Objects" implementation and NOT "LINQ to Entities" which means onces you reach to your IEnumerable.Where call, the ObjectQuery run the initial query (ctx.Orders.Include("OrderItems")) and gives the results to the IEnumerable.Where so that it will filter it out for you on the <em>Client Side</em>. <br /><br /> On the other hand the call with Queryable.Where (ctx.Orders.Include("OrderItems").Where(c => c.OrderID == id).FirstOrDefault()) will not be executed until it reach to the point that we call FirstOrDefault() function which means the Queryable.Where is then translated into native SQL along with the rest of the query and will be passed to the SQL Server, hence you see the Where clause on the SQL statement which definitely is the desired runtime behavior. <br /><br /> By the way, don't forget to import this namespace to your class file: <code><pre> using System.Linq.Expressions; </code></pre></p>
 

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