传递列表以查看C#Linq

时间:2016-11-02 08:59:08

标签: c# asp.net linq

public ActionResult Index()
{
    ESSEntities DB = new ESSEntities();
    List<EmployeeMdl> EmpList = DB.Employees.ToList();

    return View(EmpList);
}

如何通过此列表查看,因为我收到了错误

  

无法隐式转换类型&#39;&#39;到&#39; System.Collections.Generic.List&#39;

3 个答案:

答案 0 :(得分:0)

你可以这样做。

public ActionResult Index()
{
    ESSEntities DB = new ESSEntities();
    List<Employees> lstDBEmployees = DB.Employees.ToList();

List<EmployeeMdl> EmpList = new List<EmployeeMdl>();
foreach(var thisEmployee in lstDBEmployees )
{
       EmpList.Add(new EmployeeMdl(){
           prop1 = thisEmployee.prop1,
           prop2 = thisEmployee.prop2
       });
}
    return View(EmpList);
}

答案 1 :(得分:0)

我认为您的观点未收到列表请使用此。

查看:

@model List<EmployeeMdl>

@foreach(var item in Model)
{
//code here
}

控制器:

public ActionResult Index()
{
ESSEntities DB = new ESSEntities();
List<EmployeeMdl> EmpList = DB.Employees.ToList();

return View(EmpList);
}

答案 2 :(得分:0)

var EmpList = 
DB.Employees.Select(c => new{ /*your model properties somthing like c.EmployeeId, c.EmployeeName*/}).ToList();

试试这个。