views:

56

answers:

2

Hi ,

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 whether IQueryable<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 and i don't know which one is best for which situations ?

Thank you

+4  A: 

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.

Jon Skeet
@Jon: Thanks for explanation. I too got your point and would be looking for most simple example implementing both the things which will show difference among the situation specified above. I dont know how to implement interfaces in classes. i.e. I know what is interface and what functionality do they provide. But i dont know how to use them. eg. IDisposable interface, I know what it will do when implemented but how to implement is my problem same way this IQueryable interface as well
Shantanu Gupta
@Shantanu: You certainly shouldn't be implementing `IQueryable<T>` yourself. That's what LINQ to SQL (or whatever) is for.
Jon Skeet
@Jon: Could you please provide an example, could be a link representing their implementation and difference between the two. It would be a great help
Shantanu Gupta
@Shantanu this article has some useful info about IQueryable, see http://www.weirdlover.com/2010/05/11/iqueryable-can-kill-your-dog-steal-your-wife-kill-your-will-to-live-etc/
Matt Warren
@Matt: The url is quite funny but really worth a lot. I do appreciate for such a nice article as well as the writer who presented it as a chocolate. `If it's easier, think of IQueryable as an a*hole (figurative). Your college roommate, maybe. You ask him to clean the dishes. He says, "did it." You ask him to get groceries. He says, "did it." Then you say, "No you didn't." And then he does all of it all at once, and says, "Yeah I did. Right now."` **Nice Lines**
Shantanu Gupta
A: 

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().

Orsol