我有两个名为Category和Ticket的表,我想使用已经在我的数据库中的这两个表来制作我的统计表。
我已成功制作了“类别”列,但我不知道如何使我的“已发票”列工作。
My Ticket Issued列基本上将所有门票分类到各自的类别并计算它。
我需要HomeController中的帮助编码,因此我可以将两个表中的CategoryID和两个表中的一些相关联,并在About.cshtml中稍微链接以在About页面上显示该表。
HomeController.cs< - 需要在这里修改代码
public ActionResult About()
{
var data = from category in db.Categories
group category by category.Title into categoryGroup
select new CategoryGroup()
{
Title = categoryGroup.Key,
CategoryCount = categoryGroup.Count() <-- ***This needs to change into Count of Tickets which have the same category ID***
};
return View(data);
}
的ViewModels \ CategoryGroup
public class CategoryGroup
{
public int CategoryID { get; set; }
public int CategoryCount { get; set; }
public string Title { get; set; }
}
About.cshtml
@model IEnumerable<RecreationalServicesTicketingSystem.ViewModels.CategoryGroup>
@{
ViewBag.Title = "Ticket Statistics";
}
<h2>Ticket Category Statistics</h2>
<table>
<tr>
<th>
Category
</th>
<th>
Tickets Issued
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@item.CategoryCount @*Instead of Category Count I want numbers of tickets that have been issued for certain IDs. *@
</td>
</tr>
}
</table>
仅供参考
门票类
public enum Priority
{
Low,Med,High
}
public class Ticket
{
public int TicketID { get; set; }
public int CategoryID { get; set; }
public int UserID { get; set; }
public string Issue { get; set; } //A line for our issue like enrollment date in student
public Priority? Priority { get; set; }
public virtual Category Category { get; set; }
public virtual User User { get; set; }
}
类别类
public class Category
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CategoryID { get; set; }
public string Title { get; set; }
public virtual ICollection<Ticket> Tickets { get; set; }
}
答案 0 :(得分:3)
var data = from category in db.Categories
select new CategoryGroup()
{
CategoryID = category.CategoryID,
Title = category.Title,
CategoryCount = category.Tickets.Count()
};