LINQ - 按多个属性对列表进行分组,并返回带有数组成员的对象

时间:2012-06-08 16:42:38

标签: c# arrays linq group-by google-visualization

这将是一个两部分问题。

我正在尝试构建一个用于Google Charts API的数据结构(特别是他们的数据表)。

这是我现在的代码:

return Json.Encode(
    RMAs
    .Where(r => r.CreatedDate.Year > DateTime.Now.Year - 4) //Only grab the last 4 years worth of RMAs
    .GroupBy(r => new { Problem = r.Problem, Year = r.CreatedDate.Year, Quarter = ((r.CreatedDate.Month) / 3) })
    .Select(r => new { Problem = r.Key.Problem, Year = r.Key.Year, Quarter = r.Key.Quarter, Count = r.Count() })
);

这让我非常接近。这给我一个类似于以下的数组:

{"Problem":"It broke!","Year":2012,"Quarter":2,"Count":3},
{"Problem":"It broke!","Year":2012,"Quarter":1,"Count":1}

但是,我想要的是通过“问题”属性进一步对数据进行分组,以便该季度是每个问题的数组(这使得数据结构更容易迭代)。所需结构的一个例子:

{"Problem":"It broke!",
    {"Year":2012,"Quarter":2,"Count":3},
    {"Year":2012,"Quarter":1,"Count":1}
},
{"Problem":"Some other problem",
    {"Year":2012,"Quarter":1,"Count":31}
}

问题的第二部分:我如何确保每个季度都有数据(再次,这使得很多更容易迭代用于使用API​​构建数据表),甚至如果那个季度没有出现“问题”?使用与上次相同的示例:

{"Problem":"It broke!",
    {"Year":2012,"Quarter":2,"Count":3},
    {"Year":2012,"Quarter":1,"Count":1}
},
{"Problem":"Some other problem",
    {"Year":2012,"Quarter":2,"Count":0}
    {"Year":2012,"Quarter":1,"Count":31}
}

3 个答案:

答案 0 :(得分:2)

感谢Mr. TA的灵感,并告诉我您可以使用LINQ来对抗分组。

我已经在本地环境中对此进行了测试,LINQ确实返回了与一组年度/季度分组相关的问题列表。我不知道Json.Encode是否以正确的格式对其进行编码。

以下LINQ应返回符合您所需格式的匿名类型:

编辑:对于至少发生一个问题但未发生指定问题的季度,查询现在返回count = 0

var quarters = RMAs
    .Where(rma => rma.CreatedDate.Year > DateTime.Now.Year - 4)
    .GroupBy(rma => new { 
        Year = rma.CreatedDate.Year, 
        Quarter = ((rma.CreatedDate.Month) / 3) 
    });

return Json.Encode(
    RMAs
        //Only grab the last 4 years worth of RMAs
        .Where(r => r.CreatedDate.Year > DateTime.Now.Year - 4)
        // Group all records by problem     
        .GroupBy(r => new { Problem = r.Problem })
        .Select(grouping => new
            {
                Problem = grouping.Key.Problem,
                Occurrences = quarters.Select(quarter => new
                    {
                        Year = quarter.Key.Year,
                        Quarter = quarter.Key.Quarter,
                        Count = grouping
                                .GroupBy(record => new
                                {
                                    Year = record.CreatedDate.Year,
                                    Quarter = ((record.CreatedDate.Month) / 3)
                                })
                                .Where(record => 
                                    record.Key.Year == quarter.Key.Year 
                                    && record.Key.Quarter == quarter.Key.Quarter
                                ).Count()
                    }).ToArray()
            }));

更新:感谢JamieSee更新示例JSON输出:

这是JSON输出的一个示例:

[{"Problem":"P","Occurrences":[{"Year":2012,"Quarter":4,"Count":2},{"Year":2012,"Quarter":2,"Count":1},{"Year":2012,"Quarter":1,"Count":1}]},{"Problem":"Q","Occurrences":[{"Year":2012,"Quarter":3,"Count":1},{"Year":2012,"Quarter":2,"Count":1},{"Year":2012,"Quarter":1,"Count":1}]}]

答案 1 :(得分:1)

  1. 将以下内容添加到您的查询中:

    .GroupBy(x => x.Problem)
    .ToDictionary(g => g.Key, g => g.Select(x=>new { Year=x.Year, Quarter=x.Quarter, Count = x.Count }));
    
  2. 您必须在上面的.ToDictionary()之前插入以下内容:

    .Select(g =>
      new { 
        Key = g.Key, 
        Items = 
          g
            .GroupBy(r => r.Year)
            .SelectMany(gy =>
              gy.Concat(
                Enumerable.Range(1,5)
                  .Where(q => !gy.Any(r=>r.Quarter == q))
                  .Select(q => new { Problem = g.Key, Year = gy.Key, Quarter = q, Count = 0 })
              )
            )
      }
    )
    
  3. 我认为......尝试一下:)

    我建议不要采用这种方法,并在客户端创建“空”记录,以避免过多的带宽使用。

答案 2 :(得分:0)

以下是满足您所有条件的完整重述:

public static IEnumerable<DateTime> GetQuarterDates()
{
    for (DateTime quarterDate = DateTime.Now.AddYears(-4); quarterDate <= DateTime.Now; quarterDate = quarterDate.AddMonths(3))
    {
        yield return quarterDate; 
    }
}

public static void RunSnippet()
{
    var RMAs = new[] {
        new { Problem = "P", CreatedDate = new DateTime(2012, 6, 2) },
        new { Problem = "P", CreatedDate = new DateTime(2011, 12, 7) },
        new { Problem = "P", CreatedDate = new DateTime(2011, 12, 8) },
        new { Problem = "P", CreatedDate = new DateTime(2011, 8, 1) },
        new { Problem = "P", CreatedDate = new DateTime(2011, 4, 1) },
        new { Problem = "Q", CreatedDate = new DateTime(2011, 11, 11) },
        new { Problem = "Q", CreatedDate = new DateTime(2011, 6, 6) },
        new { Problem = "Q", CreatedDate = new DateTime(2011, 3, 3) }
    };

    var quarters = GetQuarterDates().Select(quarterDate => new { Year = quarterDate.Year, Quarter = Math.Ceiling(quarterDate.Month / 3.0) });

    var rmaProblemQuarters = from rma in RMAs
            where rma.CreatedDate > DateTime.Now.AddYears(-4)
            group rma by rma.Problem into rmaProblems
            select new {
                            Problem = rmaProblems.Key,
                            Quarters = (from quarter in quarters
                                        join rmaProblem in rmaProblems on quarter equals new { Year = rmaProblem.CreatedDate.Year, Quarter = Math.Ceiling(rmaProblem.CreatedDate.Month / 3.0) } into joinedQuarters
                                        from joinedQuarter in joinedQuarters.DefaultIfEmpty()
                                        select new {
                                                        Year = quarter.Year,
                                                        Quarter = quarter.Quarter,
                                                        Count = joinedQuarters.Count()
                                                   })
                       };

    string json = System.Web.Helpers.Json.Encode(rmaProblemQuarters);
    Console.WriteLine(json);
}

哪个收益率:

[{"Problem":"P","Quarters":[{"Year":2008,"Quarter":2,"Count":0},{"Year":2008,"Quarter":3,"Count":0},{"Year":2008,"Quarter":4,"Count":0},{"Year":2009,"Quarter":1,"Count":0},{"Year":2009,"Quarter":2,"Count":0},{"Year":2009,"Quarter":3,"Count":0},{"Year":2009,"Quarter":4,"Count":0},{"Year":2010,"Quarter":1,"Count":0},{"Year":2010,"Quarter":2,"Count":0},{"Year":2010,"Quarter":3,"Count":0},{"Year":2010,"Quarter":4,"Count":0},{"Year":2011,"Quarter":1,"Count":0},{"Year":2011,"Quarter":2,"Count":1},{"Year":2011,"Quarter":3,"Count":1},{"Year":2011,"Quarter":4,"Count":2},{"Year":2011,"Quarter":4,"Count":2},{"Year":2012,"Quarter":1,"Count":0},{"Year":2012,"Quarter":2,"Count":1}]},{"Problem":"Q","Quarters":[{"Year":2008,"Quarter":2,"Count":0},{"Year":2008,"Quarter":3,"Count":0},{"Year":2008,"Quarter":4,"Count":0},{"Year":2009,"Quarter":1,"Count":0},{"Year":2009,"Quarter":2,"Count":0},{"Year":2009,"Quarter":3,"Count":0},{"Year":2009,"Quarter":4,"Count":0},{"Year":2010,"Quarter":1,"Count":0},{"Year":2010,"Quarter":2,"Count":0},{"Year":2010,"Quarter":3,"Count":0},{"Year":2010,"Quarter":4,"Count":0},{"Year":2011,"Quarter":1,"Count":1},{"Year":2011,"Quarter":2,"Count":1},{"Year":2011,"Quarter":3,"Count":0},{"Year":2011,"Quarter":4,"Count":1},{"Year":2012,"Quarter":1,"Count":0},{"Year":2012,"Quarter":2,"Count":0}]}]