I started working on a project and I have noticed that ToList() gets called multiple times and I think it is not really necessary and redundant.
The first ToList() is at the data layer where the call goes to database and gets the data.
public IEnumerable<Company> GetCompanies()
{
return DbContext.Companies.ToList();
}
Then at the controller I can see another call as ToList()
public ActionResult Index()
{
var companies = _companyService.GetCompanies().ToList();
return View(companies);
}
I believe that calling ToList() in the controller is redundant. But I just wonder whether it has any impact on performance or the language itself recognizes automatically that the result is actually is already list and ignores the second call?
Update
The other question asks if there is an impact calling ToList(). However, I wonder whether there is an impact calling the ToList() multiple times for the same list of objects. As someone mentioned that that should be "hopefully ignored".