title: EF 4.1 DBContext - Apply filter loading related entities
body: <p>I'm using <code>EF 4.1 DBContext API</code>, and I'm trying to load a parent entity named <code>User</code> and then explicitly load related <code>Project</code> entities using a <code>.Where(x =&gt; x.IsEnabled)</code> filter:</p>
  
  <p>I found a suggested approach <a href="http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx" rel="nofollow">here</a> under "<strong>Applying filters when explicitly loading related entities</strong>". But I cannot figure out why <code>user.Projects</code> is not being populated. Using SQL Profiler, I have verified I'm querying <code>Projects</code> and returning the data. But it's not being loaded into my user object.</p>
  
  <p>Any thoughts?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>    public User GetUser(string userName)
      {
          try
          {
              using (var context = new Entities())
              {
                  var user = context.Users.FirstOrDefault(x =&gt; string.Compare(x.UserName, userName, true) == 0);
  
                  context.Entry(user).Collection(x =&gt; x.Projects).Query().Where(x =&gt; x.IsEnabled).Load();
  
                  //TODO: I expect user.Projects.Count() &gt; 0.
              }
          }
          catch (Exception ex)
          {
              LogWrapper.DumpException(ex);
          }
  
          return null;
      }
  </code></pre>