Differences between IEnumerable and IQueryable as ObjectSet types

.net c# entity-framework ienumerable iqueryable


According to what I understand from using LINQ extension methods (with lambda expression syntax) onIQueryable In actuality, this is an example ofObjectSet they are converted into SQL queries using LINQ. I'm referring to that command.

IQueryable<User> users = db.UserSet;
var users32YearsOld = users.Where(user => user.Age == 32);

identical to in every way

IQueryable<User> users = db.UserSet;   
var users32YearsOld = from user in users where user.Age == 32 select user;

Thus, none of them access the database before theyusers32YearsOld are listed in for a cycle or another reason. (I hope I have this properly.)

But what will happen if I don't cover it up?ObjectSet as IQueryable but asIEnumerable ? So if it is that kind,IEnumerable ?

IEnumerable<User> users = db.UserSet;
var users32YearsOld = users.Where(user => user.Age == 32);

Will it instantly touch the database, and if so, when? Which comes first, the first line or the second)? Or will it act similarly to the previous command, which delayed hitting the database untilusers32YearsOld is listed in a list? If I put the following in its place, would there be any difference?

IEnumerable<User> users = db.UserSet;
var users32YearsOld = from user in users where user.Age == 32 select user;

Many thanks

3/24/2011 8:20:39 AM

Accepted Answer

Leaving my response intact since I recently tried it and it works as I described:

Due to the lack of an enumeration, none of the stated queries will be able to access the database. The distinction betweenIQueryable search andIEnumerable the fact that in the situation ofIQueryable the filtering will be carried out on the database server as opposed toIEnumerable The filtering will be carried out in.NET code once all items have been imported into memory from the database (linq-to-objects). As you can expect, it often kills performance.

Simple test that I included in my project:

public void Test()
    // ObjectQuery<Department> converted ot IEnumerable<Department>
    IEnumerable<Department> departmetns = CreateUnitOfWork().GetRepository<Department>().GetQuery();
    // No query execution here - Enumerable has also deffered exection
    var query = departmetns.Where(d => d.Id == 1); 
    // Queries ALL DEPARTMENTS here and executes First on the retrieved result set
    var result = departmetns.First(); 
3/24/2011 9:41:50 AM

Popular Answer

Here is a brief justification:

IEnumerable<User> usersEnumerable = db.UserSet;
IQueryable<User> usersQueryable = db.UserSet;

var users = /* one of usersEnumerable or usersQueryable */;

var age32StartsWithG = users.Where(user => user.Age == 32)
                            .Where(user => user.Name.StartsWith("G");

If you employusersEnumerable , as you begin listing it off, the twoWhere s will be run in order, starting with theObjectSet will retrieve all of the items, which will then be reduced to those who are 32 years old and whose first name begins with G.

If you employusersQueryable these twoWhere When you begin iterating over s, it will transform all of the selection criteria into a query and return new objects that aggregate the selection criteria. This distinguishes itself significantly.

You often don't need to be concerned since you'll either sayvar users or ObjectSet users C# will be informed that you want to invoke the most particular function that is available when you define your variable, thusObjectSet also theIQueryable techniques for query operators (Where , Select ,...) are more particular compared to theIEnumerable methods. Nevertheless, if you distribute items to takeIEnumerable arguments, they can choose the incorrect methods to invoke.

Additionally, you may benefit from how this functions by using theAsEnumerable() and AsQueryable() ways to begin using the alternative strategy. For instance,var groupedPeople = users.Where(user => user.Age > 15).AsEnumerable().GroupBy(user => user.Age); will utilize a database query to get the appropriate users, after which the items will be locally grouped.

Nothing occurs until you start listing the sequences (with, as others have said, "with"), so it's worth reiterating that.foreach ). Now that you know why it couldn't be any other way, let's look at an example: if all results were returned at once, you couldn't construct queries to be converted into a more effective query (like an SQL query).

Related Questions


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