Linq:多列的计数值

时间:2019-04-08 10:20:51

标签: entity-framework linq

我有一个如下表:

enter image description here

现在我想要一个类似的结果 enter image description here

在单个查询中,如何准备如下所示的实体?

public BurndownJiraIssue(
            DateTime date,
            int toDoCount,
            int inProgressCount)

2 个答案:

答案 0 :(得分:2)

您可以按日期分组以获得所需的重用

    var result = list.GroupBy(x => x.Date).Select(x => new BurndownJiraIssue
    {
        Date = x.Key,
        ToDoCount = x.ToList().Count(c => c.Status == "In Progress"),
        InProgressCount = x.ToList().Count(c => c.Status == "Done"),
    });

答案 1 :(得分:0)

您可以尝试使用.GroupBy中的this overload

class Program
{
    static void Main(string[] args)
    {
        List<StatusDTO> statuses = new List<StatusDTO>
        {
            new StatusDTO
            {
                Id = 1,
                Date = "8-Apr",
                Status = "In Progress"
            },
            new StatusDTO
            {
                Id = 2,
                Date = "8-Apr",
                Status = "In Progress"
            },
            new StatusDTO
            {
                Id = 3,
                Date = "8-Apr",
                Status = "In Progress"
            },
            new StatusDTO
            {
                Id = 4,
                Date = "8-Apr",
                Status = "Done"
            },
            new StatusDTO
            {
                Id = 5,
                Date = "9-Apr",
                Status = "In Progress"
            },
            new StatusDTO
            {
                Id = 6,
                Date = "9-Apr",
                Status = "Done"
            },
            new StatusDTO
            {
                Id = 7,
                Date = "9-Apr",
                Status = "Done"
            }
        };

        List<ResultStatusDTO> results = statuses.GroupBy(x => x.Date, x => x, (a, b) => new ResultStatusDTO
        {
            Date = a,
            DoneCount = b.Count(x => x.Status == "Done"),
            InProgressCount = b.Count(x => x.Status == "In Progress")
        }).ToList();

    }
}

public class ResultStatusDTO
{
    public string Date { get; set; }
    public int InProgressCount { get; set; }
    public int DoneCount { get; set; }
}

public class StatusDTO
{
    public int Id { get; set; }
    public string Date { get; set; }
    public string Status { get; set; }
}