我的項目跟蹤每個員工使用的假期時間。我想顯示在foreach結果上方使用的小時數總和。度假指數退出選定員工的數據。MVC4 - 如何求和一個foreach列表的結果?
模型1
public class Employee
{
public int EmployeeID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public virtual ICollection<Vacation> Vacation { get; set; }
}
模型2
public class Vacation
{
public int VacationID { get; set; }
public int EmployeeID { get; set; }
[DataType(DataType.Date)]
public DateTime EventDate { get; set; }
public int Hours { get; set; }
public virtual Employee Employee { get; set; }
}
控制器模型2
public class VacationController : Controller
{
private Context db = new Context();
//
// GET: /Vacation/
public ActionResult Index([Bind(Prefix = "id")]int employeeId)
{
var Employee = db.Employee.Find(employeeId);
if (Employee != null)
{
return View(Employee);
}
return HttpNotFound();
}
查看
<table>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Employee.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.EventDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Hours)
</td>
<td>
@Html.DisplayFor(modelItem => item.Notes)
</td>
</tr>
}
</table>
使用['IEnumerable .Sum'](http://msdn.microsoft.com/zh-cn/library/bb534734.aspx) –
Styxxy