0

我有一个要填充以下内容的网格:

它正在跟踪去年每个月开放的门票。网格按优先级过滤,所以我有这样的东西:

| Mon | Critical | High | Tot |
| Jan |    5     |  2   |  7  |
| Feb |    2     |  3   |  5  |

这是我的 SQL 语句:

SELECT
    MONTH(CreateDate),
    DATENAME(MONTH,m.CreateDate) AS 'Month',
    SUM(CASE WHEN (m.PriorityId = 1) THEN 1 ELSE 0 END) AS 'Critical',
    SUM(CASE WHEN (m.PriorityId = 2) THEN 1 ELSE 0 END) AS 'High',
    SUM(CASE WHEN (m.PriorityId = 3) THEN 1 ELSE 0 END) AS 'Normal',
    SUM(CASE WHEN (m.PriorityId = 4) THEN 1 ELSE 0 END) AS 'Low',
    SUM(CASE WHEN (m.PriorityId = 5) THEN 1 ELSE 0 END) AS 'Not Ready',
    Count(m.Id) AS Total
FROM
    [projects].[dbo].[tblMaintenanceTicket] m
INNER JOIN
    [projects].[dbo].[tblPriority] p
ON
    p.Id = m.PriorityId
WHERE
    StatusId = 1
    AND YEAR(CreateDate) = year(getdate())
GROUP BY 
    MONTH(CreateDate),
    DATENAME(MONTH,m.CreateDate)
ORDER BY
    MONTH(CreateDate)

我希望在 VB LINQ 中转换它,但我不确定这是最好的方法。我也在考虑调用一个存储过程,但我真的想远离数据库。我将 MVC 3 与存储库模式一起使用,因此调用我的代码类似于 JSON 的以下输出:

Dim tickets = ticketRepo.GetAll().Include(Function(p) p.Priority).ToArray()

有什么帮助吗?

4

1 回答 1

2

这是您可以执行的操作,目前假设 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; }
  }
于 2012-11-08T13:51:56.343 回答