0

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

}

}

GRU119
  • 1,028
  • 1
  • 14
  • 31

2 Answers2

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

results is an IEnumerable<>. The function want to return only one.

return results.FirstOrDefault(); will get it to compile, but that isn't what you want, since you really want all the items in the collection.

Change the method return type -- and the @model statement in your view -- to reflect it's an IEnumerable.

James Curran
  • 101,701
  • 37
  • 181
  • 258
  • Thanks for the quick response, @James Curran . You are correct, I do need to return the entire collection. What return type would make most sense to return the entire collection? – GRU119 May 24 '17 at 17:44
  • `IEnumerable` would be fine. However, since this is pulling items from a database, you may want to stick in a `.ToList()` before the return to minimzie the time the db cnnection is open. – James Curran May 24 '17 at 17:46
  • Ok - just making sure I'm understanding. Replace return results.FirstOrDefault() to return results.ToList() OR are you saying leave the return results.FirstOrDefault() as is but add .ToList() in my LINQ query? – GRU119 May 24 '17 at 17:51
  • Use `return results.ToList()` (and change the return type) – James Curran May 24 '17 at 19:02
  • Thanks, James. I posted the full code above. Appreciate your help! – GRU119 May 25 '17 at 02:54
0

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

}

}

GRU119
  • 1,028
  • 1
  • 14
  • 31