Returning to view as ToList() not working

2019-07-06 12:17发布

问题:

I have this code:

EmployeeEntities storeDB = new EmployeeEntities();
         public ActionResult Index()
            {
                var employee = storeDB.Employees.ToList() //ToList is not showing up!
                return View(employee);
            }

and my EmployeeEntities class looks like this:

 public class EmployeeEntities : DbContext
{
    public DbSet<Employee> Employees { get; set; }
}

Why I cannot see ToList() in my ActionResult Index() ???

回答1:

Add the namespace where the .ToList() extension method is defined:

using System.Linq;

to the top of your file.



回答2:

These functions are "Extension Methods", defined in Linq. That's why you need to reference System.Linq regardless of whether you are using Entity Framework or not.

Add the namespace where the .ToList() extension method is defined:

using System.Linq;

to the top of your file.