Does the ASP.NET Web API method make sense to return an IQueryable <T>?
I am working on a project that uses a new web API and I noticed that someone is returning an IQueryable <T> from the Get method.
I understand that IQueryable is useful for improving performance (deferred execution), but I don't think the client on the other end of the HTTP connection will be able to take advantage of it.
My gut tells me that it should be IEnumberable <T> instead. Am I right about this?
source to share
Responses to methods that return IQueryable<T>
can be "queried" by passing some parameters of type odata in the query string ($ top, $ skip, $ filter, $ orderby). For example, if your resource can be found in ... / api / People, you can send these requests, which will cause the server to return different data:
.../api/People?$top=10 ==> return only the first 10 elements
.../api/People?$skip=10&$top=5 ==> return the 11th to 15th elements
.../api/People?$filter=Name%20eq%20'John' ==> Only return people named "John"
.../api/People?$orderby=Name&$top=10 ==> return the first 10 elements, ordered by name
Note that in beta, any operation that returns IQueryable<T>
automatically adds this query support. In the last bits (from codeplex), you need to manually add an attribute [Queryable]
to the operation to enable this behavior.
If you don't want this query behavior, then yes, the return is IEnumerable<T>
just fine.
source to share