我想创建一个像这样的表
(注意不确定我的sql语句是否正确)
SELECT Category, Count(TicketsIssued)
FROM Category c
LEFT JOIN Tickets t ON c.categoryID = t.categoryID
在下图中左栏是所有类别,右侧是使用相同类别ID发出的票数。 我的问题是如何在C#中编写它以放入Controllers \ HomeController.cs。
需要更改的代码是Controllers \ HomeController.cs,CategoryGroup.cs和about.cshtml。
在Controllers \ 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);
}
我希望统计信息显示在Views \ Home \ 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>
以下代码仅供参考
门票类
namespace RecreationalServicesTicketingSystem.Models
{
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; }
public Priority? Priority { get; set; }
public virtual Category Category { get; set; }
public virtual User User { get; set; }
}
}
类别类
namespace RecreationalServicesTicketingSystem.Models
{
public class Category
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CategoryID { get; set; }
public string Title { get; set; }
public virtual ICollection<Ticket> Tickets { get; set; }
}
}
\的ViewModels \ CategoryGroup.cs
namespace RecreationalServicesTicketingSystem.ViewModels
{
public class CategoryGroup
{
public int CategoryID { get; set; }
public int CategoryCount { get; set; }
public string Title { get; set; }
}
}