Returning IEnumerable vs. IQueryable
Returning IEnumerable vs. IQueryable
Returning IEnumerable vs. IQueryable
Re: Returning IEnumerable vs. IQueryable
Yes, both will give you [deferred execution](https://msdn.microsoft.com/en-us/library/bb738633(v=vs.110).aspx#Anchor_0).
The difference is that [`IQueryable`](https://msdn.microsoft.com/en-us/library/bb351562.aspx) is the interface that allows LINQ-to-SQL (LINQ.-to-anything really) to work. So if you further refine your query on an [`IQueryable`](https://msdn.microsoft.com/en-us/library/bb351562.aspx), that query will be executed in the database, if possible.
For the [`IEnumerable`](https://msdn.microsoft.com/en-us/library/9eekhta0.aspx) case, it will be LINQ-to-object, meaning that all objects matching the original query will have to be loaded into memory from the database.
In code:
```
IQueryable custs = ...;
// Later on...
var goldCustomers = custs.Where(c => c.IsGold);
```
That code will execute SQL to only select gold customers. The following code, on the other hand, will execute the original query in the database, then filtering out the non-gold customers in the memory:
```
IEnumerable custs = ...;
// Later on...
var goldCustomers = custs.Where(c => c.IsGold);
```
This is quite an important difference, and working on [`IQueryable`](https://msdn.microsoft.com/en-us/library/bb351562.aspx) can in many cases save you from returning too many rows from the database. Another prime example is doing paging: If you use [`Take`](https://msdn.microsoft.com/en-us/library/bb300906.aspx) and [`Skip`](https://msdn.microsoft.com/en-us/library/bb357513.aspx) on [`IQueryable`](https://msdn.microsoft.com/en-us/library/system.linq.iqueryable.aspx), you will only get the number of rows requested; doing that on an [`IEnumerable`](https://msdn.microsoft.com/en-us/library/9eekhta0.aspx) will cause all of your rows to be loaded in memory.
The difference is that [`IQueryable`](https://msdn.microsoft.com/en-us/library/bb351562.aspx) is the interface that allows LINQ-to-SQL (LINQ.-to-anything really) to work. So if you further refine your query on an [`IQueryable`](https://msdn.microsoft.com/en-us/library/bb351562.aspx), that query will be executed in the database, if possible.
For the [`IEnumerable`](https://msdn.microsoft.com/en-us/library/9eekhta0.aspx) case, it will be LINQ-to-object, meaning that all objects matching the original query will have to be loaded into memory from the database.
In code:
```
IQueryable custs = ...;
// Later on...
var goldCustomers = custs.Where(c => c.IsGold);
```
That code will execute SQL to only select gold customers. The following code, on the other hand, will execute the original query in the database, then filtering out the non-gold customers in the memory:
```
IEnumerable custs = ...;
// Later on...
var goldCustomers = custs.Where(c => c.IsGold);
```
This is quite an important difference, and working on [`IQueryable`](https://msdn.microsoft.com/en-us/library/bb351562.aspx) can in many cases save you from returning too many rows from the database. Another prime example is doing paging: If you use [`Take`](https://msdn.microsoft.com/en-us/library/bb300906.aspx) and [`Skip`](https://msdn.microsoft.com/en-us/library/bb357513.aspx) on [`IQueryable`](https://msdn.microsoft.com/en-us/library/system.linq.iqueryable.aspx), you will only get the number of rows requested; doing that on an [`IEnumerable`](https://msdn.microsoft.com/en-us/library/9eekhta0.aspx) will cause all of your rows to be loaded in memory.