I have some methods in my BLL that fetch some records from database and pass it to UI for binding to Data Controls such as Gridview or ...
I could choose my return data type of the methods whetherIQueryable<t>
or Ilist<t>
.
My question is which one would be better for me and why ?
Actually i don't know the difference between these two type an开发者_JS百科d i don't know which one is best for which situations ?
Thank you
The difference is that IList<T>
represents an actual list of results. It will have been "materialized" - fetched from the database, into memory. (In theory it's just an interface of course - it could still defer everything...)
IQueryable<T>
represents a query - you can still compose extra query bits afterwards, for example. You can then evaluate the query by asking for the results. This will use deferred execution, only requesting results when it actually needs them.
As for which you should return... if you're just going to bind directly to the UI, it may well make sense to convert it to a list, so that you have more control over when the query is actually executed. On the other hand, if you want to be able to tweak the query, using IQueryable<T>
will give the caller more flexibility.
I suggest ICollection or IList
IQueryable - provides functionality to evaluate queries against a specific data source wherein the type of the data is known.
In any time you can call ICollection.AsQueryable().
精彩评论