我有一个由实体框架填充的预订类,我想针对表运行不同的查询并将它们返回到视图。我已经阅读了关于使用一个类来组合它们但是我无法让它工作......
感谢您的帮助
foreach语句无法对“ITAPP.Models.Bookings”类型的变量进行操作,因为“ITAPP.Models.Bookings”不包含“GetEnumerator”的公共定义
namespace ITAPP.Models
{
public class Bookings
{
public List<tblBooking> BookedIn { get; set; }
public List<tblBooking> BookedOut { get; set; }
}
}
var tblBookings = from d in db.tblBookings.Include(t => t.Equipment).Include(t => t.tblUser)
where (d.Equipment.Bookable == true ) &&
(d.Equipment.Deleted == false) &&
(d.Equipment.DecommissionDate == null || d.Equipment.DecommissionDate == dateBlank1 || d.Equipment.DecommissionDate == dateBlank2)
select d;
Bookings Bookings = new Bookings();
Bookings.BookedIn = tblBookings.ToList();
Bookings.BookedOut = tblBookings.ToList();
return View("Loaned", Bookings);
@model ITAPP.Models.Bookings
@foreach (var item in Model) {
foreach (var inItem in item.BookedIn)
{
<td>
@Html.DisplayFor(modelItem => inItem.tblUser.FullName)
</td>
<td>
@Html.DisplayFor(modelItem => inItem.BookFrom)
</td>
<td>
@Html.DisplayFor(modelItem => inItem.BookTo)
</td>
}
}
答案 0 :(得分:2)
你改变了你的问题。对于这个错误,解决方案可能是删除外部foreach循环,这个:
@foreach (var item in Model)
因为您的View模型不是 IEnumerable 。
现在内循环将是
foreach (var inItem in Model.BookedIn)
答案 1 :(得分:0)
班级Bookings
未实施IEnumerable
,因此您无法对其进行枚举/迭代。
你想要做的事情看起来像是对我来说很糟糕,但你可以做到这一点:
public class Bookings : IEnumerable<tblBooking>
{
public List<tblBooking> BookedIn { get; set; }
public List<tblBooking> BookedOut { get; set; }
public IEnumerator<tblBooking> GetEnumerator()
{
return Union().GetEnumerator();
}
private IEnumerable<tblBooking> Union()
{
return BookedIn.Union(BookedOut);
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return Union().GetEnumerator();
}
}