2016-11-02 21 views
0
public ActionResult Index() 
{ 
    ESSEntities DB = new ESSEntities(); 
    List<EmployeeMdl> EmpList = DB.Employees.ToList(); 

    return View(EmpList); 
} 

我如何通過這個列表來查看,因爲我得到了錯誤通行證列表來查看C#的LINQ

無法隱式轉換類型「」到「System.Collections.Generic.List」

+0

你也可以顯示你的'View'嗎? –

+0

試試這個'var EmpList = DB.Employees.ToList();' – Aravind

+0

@Aravind - 那不會改變一件事情...... –

回答

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); 
} 
0

我覺得你的看法是沒有收到List請使用這個。

查看:

@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); 
} 
0
var EmpList = 
DB.Employees.Select(c => new{ /*your model properties somthing like c.EmployeeId, c.EmployeeName*/}).ToList(); 

試試這個。