This may not be specifically related to just MVC, but in a lot of models, repository interfaces etc, i see a lot of properties from time to time defined as IQueryable<t>... IENumerable... (usually on some kind of a relationship property) and other types
of collections besides simple lists.
What is IQueryable and when would you use it? I can't seem to find any simple intro's regarding some of these different interfaces, and when to use them.
The definition of IQueryable is to query against a known data source with unspecified data types. I don't think it's common to use IQueryable.
Actually it's pretty common with the repository pattern, especially since you do not want to apply LINQ filtering (where, etc) against an IEnumerable.
rockitdev - An IQueryable is basically an expression that is evaulated and executed againts a specific store. The library being used is responsible for executing this expression. Think of it as almost like query.
An IEnumerable on the other hand is actual data, it's like an array with objects inside of it. IQueryable doest not have any data just the description of the query. Once you enumerate an IQueryable (by calling ToList or another emuerattion method or operation)
then the query is executed and data is returned.
It's important to know the difference because when filtering data using a where clause you want to do this against an IQueryable and NOT an IEnumerable. If you apply a where clause to an IQueryable the where is made part of the expression and when evaluated
will cause that expression to be executed on the target. For eample if you are using an ORM like Entity Framework, that where clause will be translated into a Where statement in SQL. This is great because it means your database is doing filtering and that
is a very fast operation.
Now if you did not apply the where to the IQueryable and called ToList beforehand to get an IEnumerable, now applying the where clause will happen in code AFTER all your data is pulled ffrom the database. Meaning if you have a million records, EF will get
all 1 million and the filtering will happen in code. This is horrible for performance and can cause your app to run VERY slowly.
rockitdev
Member
5 Points
32 Posts
Collection Types
Feb 22, 2013 06:37 PM|LINK
This may not be specifically related to just MVC, but in a lot of models, repository interfaces etc, i see a lot of properties from time to time defined as IQueryable<t>... IENumerable... (usually on some kind of a relationship property) and other types of collections besides simple lists.
What is IQueryable and when would you use it? I can't seem to find any simple intro's regarding some of these different interfaces, and when to use them.
JohnLocke
Contributor
3752 Points
809 Posts
Re: Collection Types
Feb 22, 2013 06:48 PM|LINK
The definition of IQueryable is to query against a known data source with unspecified data types. I don't think it's common to use IQueryable.
You may want to become familiar with IEnumerable, which is a generic collection of data. You can form Lists from an IEnumerable.
An ICollection is another type of generic collection of items, and can commonly be used when editing a generic list of items.
List and IList are both collection methods as well.
CodeHobo
All-Star
18669 Points
2648 Posts
Re: Collection Types
Feb 23, 2013 12:41 AM|LINK
Actually it's pretty common with the repository pattern, especially since you do not want to apply LINQ filtering (where, etc) against an IEnumerable.
rockitdev - An IQueryable is basically an expression that is evaulated and executed againts a specific store. The library being used is responsible for executing this expression. Think of it as almost like query.
An IEnumerable on the other hand is actual data, it's like an array with objects inside of it. IQueryable doest not have any data just the description of the query. Once you enumerate an IQueryable (by calling ToList or another emuerattion method or operation) then the query is executed and data is returned.
It's important to know the difference because when filtering data using a where clause you want to do this against an IQueryable and NOT an IEnumerable. If you apply a where clause to an IQueryable the where is made part of the expression and when evaluated will cause that expression to be executed on the target. For eample if you are using an ORM like Entity Framework, that where clause will be translated into a Where statement in SQL. This is great because it means your database is doing filtering and that is a very fast operation.
Now if you did not apply the where to the IQueryable and called ToList beforehand to get an IEnumerable, now applying the where clause will happen in code AFTER all your data is pulled ffrom the database. Meaning if you have a million records, EF will get all 1 million and the filtering will happen in code. This is horrible for performance and can cause your app to run VERY slowly.
Blog | Twitter : @Hattan