willvv
willvv

Reputation: 8649

ASP.NET MVC, EF: Make action methods return List instead of IEnumerable

I have several actions in my controllers that return an IEnumerable of the EF entities.

I was thinking on making these actionmethods return a list instead (just calling a model.ToList() before passing it back to the view).

There are two reasons why I'm thinking on doing that:

My question is, are any drawbacks of doing this? I'm pretty sure that every IEnumerable Model will be traversed completely, so it won't be a problem retrieving data that won't be used.

Thanks for your help.

Upvotes: 0

Views: 270

Answers (1)

Justin Pihony
Justin Pihony

Reputation: 67115

This is a perfectly valid thing to do IMO. You pretty much already pointed out the pros and cons. The main difference is that .ToList is loaded into memory all at once, while the other allows for an on-demand load.

If you are planning to load the entire enumeration anyway, then calling .ToList will save you bandwidth by requesting everything in one grab instead of one at a time. So, if anything this sounds like it might be a better path anyway.

Upvotes: 2

Related Questions