Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework and Repository Pattern (problem with IQueryable)
    primarykey
    data
    text
    <p>I just switched from Linq 2 SQL to Entity Framework, and I'm seeing some strange behaviors in EF that I'm hoping someone can help with. I tried Googling around, but I wasn't able to find other people with this same problem. I've mocked up a scenario to explain the situation.</p> <p>If I work directly with an EF context, I'm able to do a select within a select. For example, this executes perfectly fine:</p> <pre><code> // this is an Entity Framework context that inherits from ObjectContext var dc = new MyContext(); var companies1 = (from c in dc.Companies select new { Company = c, UserCount = (from u in dc.CompanyUsers where u.CompanyId == c.Id select u).Count() }).ToList(); </code></pre> <p>However, if I use a repository pattern where the repository is returning IQueryable (or even ObjectSet or ObjectQuery), I get a NotSupportedException (LINQ to Entities does not recognize the method 'System.Linq.IQueryable`1)...</p> <p>Here is an example of my repository:</p> <pre><code>public class Repository { private MyContext _dc; public Repository() { _dc = new MyContext(); } public IQueryable&lt;Company&gt; GetCompanies() { return _dc.Companies; } public IQueryable&lt;CompanyUser&gt; GetCompanyUsers() { return _dc.CompanyUsers; } } </code></pre> <p>// I'm using the repository inside another class (e.g. in my Services layer)</p> <pre><code> var repository = new Repository(); var companies2 = (from c in repository.GetCompanies() select new { Company = c, UserCount = (from u in repository.GetCompanyUsers() where u.CompanyId == c.Id select u).Count() }).ToList(); </code></pre> <p>The above code throws a NotSupportedException.</p> <p>I realize that if there's an association between Companies and CompanyUsers, then I can simply do this and it will work fine:</p> <pre><code> var companies3 = (from c in repository.GetCompanies() select new { Company = c, UserCount = (from u in c.CompanyUsers select u).Count() }).ToList(); </code></pre> <p>...but my example is just a simplified version of a more complicated scenario where I don't have an association between the entities.</p> <p>So I'm very confused why Entity Framework is throwing the NotSupportedException. How is it that the query works perfectly fine when I'm working with the EF context directly, but it's not supported if I'm working with IQueryable returned from another method. This worked perfectly fine with Linq 2 SQL, but it doesn't seem to work in Entity Framework.</p> <p>Any insight would be greatly appreciated.</p> <p>Thanks in advance. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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