Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework - Querying from ObjectContext vs Querying from Navigation Property
    text
    copied!<p>I've noticed that depending on how I extract data from my Entity Framework model, I get different types of results. For example, when getting the list of employees in a particular department:</p> <p>If I pull directly from ObjectContext, I get an <code>IQueryable&lt;Employee&gt;</code>, which is actually a <code>System.Data.Objects.ObjectQuery&lt;Employee&gt;</code>:</p> <pre><code>var employees = MyObjectContext.Employees.Where(e =&gt; e.DepartmentId == MyDepartment.Id &amp;&amp; e.SomeCondtition) </code></pre> <p>But if I use the Navigation Property of MyDepartment, I get an <code>IEnumerable&lt;Employee&gt;</code>, which is actually a <code>System.Linq.WhereEnumerableIterator&lt;Employee&gt;</code> (private class in System.Linq.Enumerable):</p> <pre><code>var employees = MyDeparment.Employees.Where(e =&gt; e.SomeCondtition) </code></pre> <p>In the code that follows, I heavily use <code>employees</code> in several LINQ queries (<code>Where</code>, <code>OrderBy</code>, <code>First</code>, <code>Sum</code>, etc.)</p> <p><strong>Should I be taking into consideration which query method I use? Will there be a performance difference? Does the latter use deferred execution? Is one better practice? Or does it not make a difference?</strong></p> <p>I ask this because since installing ReShaper 6, I'm getting lots of <em>Possible multiple enumeration of IEnumerable</em> warnings when using the latter method, but none when using direct queries. I've been using the latter method more often, simply because it's much cleaner to write, and I'm wondering if doing so has actually had a detrimental effect!</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