Return LINQ 将 ViewModel 从业务逻辑层填充到控制器 - MVC

Return LINQ populated ViewModel from Business Logic Layer to Controller - MVC

我有一个 LINQ 查询,它填充 StaffHierarchySearchViewModel 的视图模型。我正在尝试在我的业务逻辑层 (BLL) 中填充 StaffHierarchySearchViewModel,并将这些结果 return 发送到我的控制器。当我尝试在我的方法中 return results 值时,我在 return 结果行上收到错误。我收到以下错误:Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<WebReportingToolDAL.Models.ViewModels.StaffHierarchySearchViewModel>' to 'WebReportingToolDAL.Models.ViewModels.StaffHierarchySearchViewModel'. An explicit conversion exists (are you missing a cast?)' 我尝试了其他解决方法,但我似乎仍然无法使 return 值起作用。

namespace WebReportingToolBLL
{
 public class StaffHierarchySearch
  {
    private Collections_StatsEntities db = new Collections_StatsEntities();

    public StaffHierarchySearchViewModel StaffHierarchySearchViewModel(string SearchString)
    {
        IEnumerable<StaffHierarchySearchViewModel> results = from sa in db.SecurityAccesses
                     join ss in db.SecuritySystems on sa.System equals ss.SystemID
                     join s in db.Staffs on sa.EmpID equals s.EmpID
                     join u in db.Units on s.UnitID equals u.UnitID
                     join h in db.Hierarchies on sa.EmpID equals h.EmpID
                     join t in db.Staffs on h.TeamID equals t.EmpID
                     where sa.ID == SearchString
                     select new WebReportingToolDAL.Models.ViewModels.StaffHierarchySearchViewModel
                     {
                         ID = sa.ID,
                         SystemName = ss.SystemName,
                         LastName = s.LastName,
                         FirstName = s.FirstName,
                         UnitName = u.UnitName,
                         EffectiveStart = h.EffStart,
                         EffectiveEnd = h.EffEnd,
                         ManagerFirstName = t.FirstName,
                         ManagerLastName = t.LastName,
                         Search = SearchString
                     };

        return results;
    }

}

}

public StaffHierarchySearchViewModel StaffHierarchySearchViewModel(string SearchString)
{
    IEnumerable<StaffHierarchySearchViewModel> results = from sa in db.SecurityAccesses
 /// snip 
    return results;
}

results 是一个 IEnumerable<>。该函数想要 return 只有一个。

return results.FirstOrDefault(); 会编译它,但这不是你想要的,因为你真的想要集合中的所有项目。

更改方法 return 类型——以及您视图中的 @model 语句——以反映它是一个 IEnumerable。

根据@James Curran 所说,我将方法 return 类型更新为 List<StaffHierarchySearchViewModel> 并添加 return results.ToList(); 它似乎按预期工作。

namespace WebReportingToolBLL
{
public class StaffHierarchySearch
{
    private Collections_StatsEntities db = new Collections_StatsEntities();

    public List<StaffHierarchySearchViewModel> StaffHierarchySearchViewModel(string SearchString)
    {
        IEnumerable<StaffHierarchySearchViewModel> results = from sa in db.SecurityAccesses
                     join ss in db.SecuritySystems on sa.System equals ss.SystemID
                     join s in db.Staffs on sa.EmpID equals s.EmpID
                     join u in db.Units on s.UnitID equals u.UnitID
                     join h in db.Hierarchies on sa.EmpID equals h.EmpID
                     join t in db.Staffs on h.TeamID equals t.EmpID
                     where sa.ID == SearchString
                     select new WebReportingToolDAL.Models.ViewModels.StaffHierarchySearchViewModel
                     {
                         ID = sa.ID,
                         SystemName = ss.SystemName,
                         LastName = s.LastName,
                         FirstName = s.FirstName,
                         UnitName = u.UnitName,
                         EffectiveStart = h.EffStart,
                         EffectiveEnd = h.EffEnd,
                         ManagerFirstName = t.FirstName,
                         ManagerLastName = t.LastName,
                         Search = SearchString
                     };

        return results.ToList();
    }

}

}