What is the difference between returning IQueryable
vs. IEnumerable
, when should one be preferred over the other?
In general terms I would recommend the following:
Return IQueryable
if you want to enable the developer using your method to refine the query you return before executing.
Return IEnumerable
if you want to transport a set of Objects to enumerate over.
Imagine an IQueryable
as that what it is - a "query" for data (which you can refine if you want to). An IEnumerable
is a set of objects (which has already been received or was created) over which you can enumerate.