在LINQ中将结果分组

时间:2019-04-16 16:26:28

标签: c# linq

我目前正在返回一个包含事件名称和发生日期的列表。我按月和年分组返回的列表。我需要在该列表中再添加一个项目,这是另一个按事件名称和每个事件总计分组的列表。我按年和月分组,第二部分有问题。

我的模特:

public class IncidentTrendList
{
    public int Year { get; set; }
    public int Month { get; set; }
    public List<IncidentList> IncidentList { get; set; }
}

public class IncidentList
{
    public int IncidentTotal { get; set; }
    public string IncidentName { get; set; }
}

public class IncidentRiskMatrix
{
    public DateTime Date { get; set; }
    public string IncidentName { get; set; }
}

按逻辑分组:

  var groupedList = IncidentRiskMatrix
                  .GroupBy(u => new
                  {
                     Month = u.Date.Month,
                     Year = u.Date.Year,
                  })
                 .Select(grp => new IncidentTrendList
                  {
                     Month = grp.Key.Month,
                     Year = grp.Key.Year,
                     IncidentList ---> this is a list
                  }).ToList();

分组依据之后,在.IncidentList中选择。我将如何对事件名称和每个事件总计进行分组,然后将该项目添加到“ IncidentList”列表中。

2 个答案:

答案 0 :(得分:2)

如果您更恰当地命名课程:

public class IncidentTrend
{
    public int Year { get; set; }
    public int Month { get; set; }
    public List<IncidentsByType> IncidentsByType { get; set; }
}

public class IncidentsByType
{
    public int Total { get; set; }
    public string Name { get; set; }
}

public class IncidentRiskMatrix
{
    public DateTime Date { get; set; }
    public string IncidentName { get; set; }
}

答案变得更加明显:

var groupedList = incidentsRiskMatrix
    .GroupBy(u => new
    {
        u.Date.Month,
        u.Date.Year
    })
    .Select(grp => new IncidentTrend
    {
        Month = grp.Key.Month,
        Year = grp.Key.Year,
                       // from each group
        IncidentsByType = grp
            // group the items by their IncidentName
            .GroupBy(x => x.IncidentName)
            // and select new IncidentByType
            .Select(x => new IncidentsByType
            {
                // by getting the amount of items in the group
                Total = x.Count(),
                // and the key of the group
                Name = x.Key
            })
            .ToList()
    })
    .ToList();

答案 1 :(得分:0)

尝试:

var groupedList = new List<IncidentRiskMatrix>() //change this to the list of IncidentRiskMatrix variable
                  .GroupBy(u => new
                  {
                     Month = u.Date.Month,
                     Year = u.Date.Year,
                  })
                 .Select(grp => new IncidentTrendList
                 {
                    Month = grp.Key.Month,
                    Year = grp.Key.Year,
                    IncidentList = grp.GroupBy(x => x.IncidentName).Select(y => new IncidentList()
                                                  {
                                                     IncidentName = y.Key,
                                                     IncidentTotal = y.Count()
                                                  }
                                             ).ToList()
                 }).ToList();