i have the following model method inside my asp.net MVc web application:-
public IQueryable<User> searchusers(string q, int id)
{
return from u in entities1.Users
where (!u.Users_Classes.Any(c => c.ClassID == id) && (u.UserID.Contains(q))
select u;
}
which will be called using the following action method:-
[AcceptVerbs(HttpVerbs.Post)]
public PartialViewResult Search(string q, int classid)
{
var users = r.searchusers(q, classid).ToList();
ViewBag.id = classid;
// code does here
}
now if i remove the .ToList() from my action method the code will still work fine,, so will using the .ToList() method bring any advantages or features ?
BR
Edit:-
here is the full code for my action method:-
[AcceptVerbs(HttpVerbs.Post)]
public PartialViewResult Search(string q, int classid)
{
var users = r.searchusers(q, classid).ToList();
ViewBag.id = classid;
return PartialView("_usersearch", users);
}
When you call ToList, you ask the Entity Framework to execute the query immediately, and then you will work with in memory collection. Otherwise, the query will be executed when you loop through the result.