Neil Knight
Neil Knight

Reputation: 48567

Is IEnumerable a good return type for data that allows filtering?

I just read this question and it got me thinking of why I would need to use the IEnumerable when retrieving data. I understand the differences between IQueryable and IEnumerable, but would an IEnumerable object be better for data that allows filtering? for example, a table with the records which contain a date, so I can sort on the date.

Upvotes: 1

Views: 290

Answers (2)

sebastianmehler
sebastianmehler

Reputation: 1033

It depends.

If you want to First recieve all Data and afterwards filter the returned Set of Objects use IEnumerable.

If you want to enable Filtering on the Database (e.g. Linq-to-sql / Entity Framework) better use IQueryable.

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 190976

If you have the objects in memory - not from another data source such as a database - use IEnumerable<T>. That way the built in LINQ to Objects will work automatically.

You could even extend LINQ to Objects by writing custom extension methods and using yield return and yield break.

If you are using Entity Framework or some other system that is using IQueryable<T>, I would keep it as IQueryable<T> until you need it as objects.

Upvotes: 4

Related Questions