我有一个LINQ查询,用于填充 StaffHierarchySearchViewModel 的视图模型。我试图在我的业务逻辑层(BLL)中填充 StaffHierarchySearchViewModel 并将这些结果返回给我的控制器。当我尝试在我的方法中返回结果值时,我在返回结果行上收到错误。我收到错误: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?)'
我尝试了其他解决方法,但我仍然无法获得返回值。
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;
}
}
}
答案 0 :(得分:1)
public StaffHierarchySearchViewModel StaffHierarchySearchViewModel(string SearchString)
{
IEnumerable<StaffHierarchySearchViewModel> results = from sa in db.SecurityAccesses
/// snip
return results;
}
results
是IEnumerable<>
。该函数只想返回一个。
return results.FirstOrDefault();
会让它进行编译,但这并不是你想要的,因为你真的想要集合中的所有项目。
更改方法返回类型 - 以及视图中的@model
语句 - 以反映它是IEnumerable。
答案 1 :(得分:0)
关于@James Curran所说的内容,我将方法返回类型更新为def do_this (a,b,c)
puts 1.to_s, 2.to_s,3.to_s
if a == 4
do_this(1,2,3)
end
end
do_this(4,5,6)
并添加List<StaffHierarchySearchViewModel>
它似乎按预期工作。
return results.ToList();
}