title: EF: Include with where clause
body: <p>As the title suggest I am looking for a way to do a where clause in combination with an include.</p>
  
  <p>Here is my situations:
  I am responsible for the support of a large application full of code smells.
  Changing too much code causes bugs everywhere so I am looking for the safest solution.</p>
  
  <p>Let's say I have an object Bus and an object People(Bus has a navigation prop Collection of People).
  In my Query I need to select all the Busses with only the Passengers that are awake. This is a simplistic dummy example</p>
  
  <p>In the current code:</p>
  
  <pre><code>var busses = Context.Busses.Where(b=&gt;b.IsDriving == true);
  foreach(var bus in busses)
  {
     var passengers = Context.People.Where(p=&gt;p.BusId == bus.Id &amp;&amp; p.Awake == true);
     foreach(var person in passengers)
     {
         bus.Passengers.Add(person);
     }
  }
  </code></pre>
  
  <p>After this code the Context is disposed and in the calling method the resulting Bus entities are Mapped to a DTO class (100% copy of Entity).</p>
  
  <p>This code causes multiple calls to DB which is a No-Go, so I found this solution <a href="http://blogs.msdn.com/b/alexj/archive/2009/10/13/tip-37-how-to-do-a-conditional-include.aspx">ON MSDN Blogs</a></p>
  
  <p>This worked great when debugging the result but when the entities are mapped to the DTO (Using AutoMapper) I get an exception that the Context/Connection has been closed and that the object can't be loaded. (Context is always closed can’t change this :( )</p>
  
  <p>So I need to make sure that the Selected Passengers are already loaded (IsLoaded on navigation property is also False). If I inspect the Passengers collection The Count also throws the Exception but there is also a collection on the Collection of Passegers called “wrapped related entities” which contain my filtered objects.</p>
  
  <p>Is there a way to load these wrapped related entities into the whole collection?
  (I can't change the automapper mapping config because this is used in the whole application).</p>
  
  <p>Is there another way to Get the Active Passengers?</p>
  
  <p>Any hint is welcome...</p>
  
  <h2>Edit</h2>
  
  <p>Answer of Gert Arnold doesn't work because the data isn't loaded eagerly.
  But when I simplify it and delete the where it is loaded. This is realy strange since the execute sql returns all the passengers in both cases. So there must be a problem when putting the results back into the entity.</p>
  
  <pre><code>Context.Configuration.LazyLoadingEnabled = false;
  var buses = Context.Busses.Where(b =&gt; b.IsDriving)
          .Select(b =&gt; new 
                       { 
                           b,
                           Passengers = b.Passengers
                       })
          .ToList()
          .Select(x =&gt; x.b)
          .ToList();
  </code></pre>
  
  <h2>Edit2</h2>
  
  <p>After a lot of struggle the answer of Gert Arnold work!
  As Gert Arnold suggested you need to disable Lazy Loading and Keep it OFF.
  This will ask for some extra changes to the appliaction since the prev developer loved Lazy Loading -_-</p>