Reputation: 8291
I understand how IEnumerable and IQueryable works. I just cannot imagine a situation where IEnumerable would be needed in entity framework when working with SQL database. I wonder if I can just discard IEnumerable in EF. Can you provide any useful example that shows IEnumerable could be more useful than IQueryable?
Upvotes: 0
Views: 130
Reputation: 155648
Provided a data source (as IQueryable
) can be queried, then yes, use IQueryable
- though you shouldn't be creating IQueryable
instances or implementing it yourself, that's what EF is for. You will still need to use IEnumerable
as method parameters or return types if you're using EF with external data-sources or other data that isn't queryable itself, such as JOIN
ing an EF table with non-EF data.
For example, you'd have a return type as IEnumerable<T>
if the data you're returning isn't queryable because you called AsEnumerable
or ToList
but you don't want to reveal implementation details - but I'd then prefer IReadOnlyList<T>
in that case.
Upvotes: 1
Reputation: 117175
There are three situations that come to mind.
Upvotes: 1