2

I'm new in ASP.NET MVC. Now, I've searched in the internet what does the .toList() do.

Using ToList() method to force LINQ query execute immediately

calling ToList() method, you force LINQ to execute immediately and return actual results.

But what's the difference between these two.

return View(db.Items) and return View(db.Items.ToList())

Because I can still see the same result in my page.

Jojo
  • 1,875
  • 3
  • 29
  • 29
Boy Pasmo
  • 8,021
  • 13
  • 42
  • 67

1 Answers1

4

I guess View(db.Items.ToList()) statement first execute ToList() method and get result from database and pass to the view.

and View(db.Items) first call view with LINQ query and view will call ToList method to get list from database (late binding).

Sameer
  • 2,143
  • 1
  • 16
  • 22
  • So which is more preferred? So you mean, once you pass results to the view whether its parse or not, it will execute `.ToList()`? – Boy Pasmo Feb 06 '14 at 11:13
  • 1
    Have a look here: http://stackoverflow.com/questions/3628425/ienumerable-vs-list-what-to-use-how-do-they-work But basically forcing the query to execute immediately by using `ToList()` you may lose some of the benefits of deferred execution (by not using `ToList()`) and possibly some other benefits. But check out the post, it may help. Also, you may not want to hold all objects in memory first then work with them: http://stackoverflow.com/questions/334579/do-you-tolist – Ric Feb 06 '14 at 11:16