I have a LINQ query that populates a view model of StaffHierarchySearchViewModel. I'm trying to populate StaffHierarchySearchViewModel in my Business Logic Layer (BLL) and return those results to my controller. When I attempt to return the results value in my method, I'm getting errors on the return results line. I'm getting errors of: 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?)'
I've tried other workarounds, but I still can't seem to get the return value to work.
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;
}
}
}
Going off of what @James Curran said, I updated the method return type to List<StaffHierarchySearchViewModel>
as well as add return results.ToList();
It seems to be working as expected.
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();
}
}
}