IQueryable vs. IEnumerable in the.NET Entity Framework

entity entity-framework frameworks ienumerable iqueryable

Question

Observe this line of code, please. This is a stored procedure call that generates anObjectResult<long?> . I added the Select: to extract the long values.

dbContext.FindCoursesWithKeywords(keywords).Select(l => l.Value);

This Select returns intellisense-based information.IEnumerable<long> .

I'm not sure whether I simply assumed this from reading it someplace, but I've always believed that when the EF API returns anIEnumerable (but notIQueryable ), then this indicates that the predicted outcomes have come to pass. i.e., they were retrieved from the database.

Today I learned that I was mistaken (or maybe that's a bug?). I keep experiencing the issue

"New transaction is not allowed because there are other threads running in the session"

This error basically informs you that you are attempting to save changes while the database reader is still reading records.

Eventually, I added (what I thought was a long shot) and fixed the problem.ToArray() appeal for the realization ofIEnumerable<long> ...

In conclusion, what should I anticipate?IEnumerable findings from EF to include unrealized results as well? If so, is there a means to determine if anIEnumerable has come to pass or not?

I apologize if this is one of those "duhhh" queries. Thank you.

1
24
9/30/2011 6:39:20 AM

Accepted Answer

IQueryable is used when employing Linq-to-entities. This means that you construct declarative LINQ queries in your application, which the LINQ provider will translate as SQL and execute on the server. The query will become IEnumerable after it has been performed (iterated), and objects will only be materialized when necessary for the iteration—not right away.

As soon as you call a stored procedure, your application is not leveraging Linq-to-entities since there is no declarative query. You are just calling an existing SQL query that is already stored on the database server. It will come back.IEnumerable But once again, not all outcomes will manifest right away. Results will manifest as they are iterated. When you expressly request obtaining an object, a database cursor or.NET data reader will work according to this approach.

Thus, if you refer to anything as follows:

foreach (var keyword in dbContext.FindCoursesWithKeywords(keywords)
                                 .Select(l => l.Value))
{
    ...   
}

You are downloading courses one at a time (why load the whole course if you are just interested in the keywords?). Your data reader is opened to retrieve records until you close it or end the loop.

If you were to rename this:

foreach (var keyword in dbContext.FindCoursesWithKeywords(keywords)
                                 .ToList() // or ToArray 
                                 .Select(l => l.Value))
{
    ...
}

You will make the query materialize every result right away, and the loop will run on a collection in memory rather than an open database reader.

difference betweenIEnumerable and IQueryable is not the method of obtaining data becauseIQueryable is IEnumerable The distinction is in the backing design (something must implement these interfaces).

31
9/30/2011 9:17:44 AM

Popular Answer

completing anIEnumerable<T> says that all subsequent actions, such as linq-to-objects, will take place in C# code. The query has not yet been run, despite what this indicates.

All remaining data must now be acquired from the database and transmitted to.net after you have degraded to linq-to-objects. Since linq-to-objects may run any C# code and is not limited by what your linq provider can convert to SQL, it is more versatile but can significantly reduce performance (for example, database indexes won't be utilized by linq-to-objects).

A IEnumerable<T> might be data that has already been materialized or a postponed query. Typically, the default linq operators are postponed, andToArray() /ToList() always become manifested.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow