这是您可以执行的操作,目前假设 StatusId 在 tblMaintenanceTicket 表中。
视觉基础:
Module Module1
Sub Main()
Dim rand As New Random
Dim list As New List(Of tblMaintenanceTicket)
For count = 1 To 100
Dim newTicket As New tblMaintenanceTicket
newTicket.CreateDate = Now.AddDays(rand.Next(0, 365) * -1)
newTicket.PriorityId = rand.Next(1, 6)
newTicket.StatusId = 1
list.Add(newTicket)
Next
Dim results = From ticket In list _
Where ticket.StatusId = 1 And ticket.CreateDate.Year = Now.Year _
Group ticket By ticket.CreateDate.Month Into Group _
Select New TicketResults With { _
.Month = Month,
.Critical = Group.Count(Function(t) t.PriorityId = 1),
.High = Group.Count(Function(t) t.PriorityId = 2),
.Normal = Group.Count(Function(t) t.PriorityId = 3),
.Low = Group.Count(Function(t) t.PriorityId = 4),
.NotReady = Group.Count(Function(t) t.PriorityId = 5)
}
End Sub
Public Class tblMaintenanceTicket
Public CreateDate As Date
Public PriorityId As Integer
Public StatusId As Integer
End Class
Public Class TicketResults
Public Month As Integer
Public Critical As Integer
Public High As Integer
Public Normal As Integer
Public Low As Integer
Public NotReady As Integer
End Class
End Module
C#:
class Program
{
static void Main(string[] args)
{
//Generate some random data
var list = new List<tblMaintenanceTicket>();
var rand = new Random();
for (var count = 0; count < 100; ++count)
{
var newTicket = new tblMaintenanceTicket();
newTicket.CreateDate = DateTime.Now.AddDays(rand.Next(0, 365)* -1);
newTicket.PriorityId = rand.Next(1, 6);
newTicket.StatusId = 1;
list.Add(newTicket);
}
var results = from ticket in list
where ticket.StatusId == 1 && ticket.CreateDate.Year == DateTime.UtcNow.Year
group ticket by ticket.CreateDate.Month into months
select new TicketResults
{
Month = months.Key,
Critical = months.Count(m => m.PriorityId == 1),
High = months.Count(m => m.PriorityId == 2),
Normal = months.Count(m => m.PriorityId == 3),
Low = months.Count(m => m.PriorityId == 4),
NotReady = months.Count(m => m.PriorityId == 5)
};
System.Diagnostics.Debugger.Break();
}
}
public class TicketResults
{
public int Month { get; set; }
public int Critical { get; set; }
public int High { get; set; }
public int Normal { get; set; }
public int Low { get; set; }
public int NotReady { get; set; }
}
public class tblMaintenanceTicket
{
public DateTime CreateDate { get; set; }
public int PriorityId { get; set; }
public int StatusId { get; set; }
}