Note that there are some explanatory texts on larger screens.

plurals
  1. PO.NET Entity Framework - IEnumerable VS. IQueryable
    text
    copied!<p>Please see this line of code. This is an invocation of a stored procedure, which returns an <code>ObjectResult&lt;long?&gt;</code>. In order to extract the long values I added the Select:</p> <pre><code>dbContext.FindCoursesWithKeywords(keywords).Select(l =&gt; l.Value); </code></pre> <p>Based on intellisense this Select returns <code>IEnumerable&lt;long&gt;</code>.</p> <p>I'm not sure whether I read it somewhere or maybe just got used to this assumption - I always thought that when the EF API returns an <code>IEnumerable</code> (and not <code>IQueryable</code>) then this means that the results have been materialized. Meaning they've been pulled from the database.</p> <p>I found out today that I was wrong (or maybe that's a bug?). I kept getting the error </p> <blockquote> <p>"New transaction is not allowed because there are other threads running in the session"</p> </blockquote> <p>Basically, this error tells you that you're trying to save changes while the db reader is still reading records.</p> <p>Eventually I solved it by (what I considered a long shot) and added <code>ToArray()</code> call to materialize the <code>IEnumerable&lt;long&gt;</code>...</p> <p>So - the bottom line - should I expect <code>IEnumerable</code> results from EF to contain results that haven't materialized yet? If yes then is there a way to know whether an <code>IEnumerable</code> has been materialized or not?</p> <p>Thanks and apologies if this is one of those 'duhhh' questions... :)</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