title: Using .Include() when joining a view using Entity Framework
body: <p>Im using an Indexed View which is an entity without any relations (relations to my table entities would be preferable but this seemed to be nearly impossible to achieve). This view consists of 4 FK's which together form the PK:</p>
  
  <pre><code>PortalID
  CategoryID
  BranchID
  CompanyID
  </code></pre>
  
  <p>Now im joining this view to select a set of companies like this:</p>
  
  <pre><code>var companies = (from c in database.Companies.FindAll().Include("City")
                   join l in database.CompanyList.FindAll() on c.ID equals l.CompanyID
                   where l.PortalID == 9 &amp;&amp; l.BranchID == 1597
                   select c).Take(10);
  </code></pre>
  
  <p>The entity Company has an association with the table Cities (CityID, City), which i want to Include and works perfectly. However when i join the view the .Include() is ignored completely and thus results in a query without a join to the cities table.</p>
  
  <p>As you might have concluded im using a repository pattern and FindAll() returns an IQueryable and ive manually added the Include extension to IQueryable like this:</p>
  
  <pre><code>public static IQueryable&lt;T&gt; Include&lt;T&gt;(this IQueryable&lt;T&gt; sequence, string path) {
      var objectQuery = sequence as ObjectQuery&lt;T&gt;;
      if (objectQuery != null)
          return objectQuery.Include(path);
  
      return sequence;
  }
  </code></pre>
  
  <p>Ive thoroughly tested this extension and works like it should. </p>
  
  <p>So now for my question, why is it ignoring the Include operation in the final expression tree and how can i prevent it from doing so?
  <br />
  <br />
  <strong>UPDATE:</strong><br />
  I got my solution from the following link:
  <a href="http://blogs.msdn.com/b/alexj/archive/2009/06/02/tip-22-how-to-make-include-really-include.aspx" rel="noreferrer">http://blogs.msdn.com/b/alexj/archive/2009/06/02/tip-22-how-to-make-include-really-include.aspx</a></p>
  
  <p>Which says i need to rewrite my query like this:</p>
  
  <pre><code>var companies = (from c in database.Companies.GetAll()
                   join l in database.CompanyList.GetAll() on c.ID equals l.CompanyID
                   where l.PortalID == 9 &amp;&amp; l.BranchID == 1597
                   select c).Include("City").Take(10);
  </code></pre>