曾经试图找到一种合适的解决方案,但到目前为止仍无法理解或找到一种合适的解决方案。
具有以下数据库:
我正在尝试获取每月的点击次数。 所以我会得到类似的东西:
--------------
January - 10
February - 7
March - 22
etc.
到目前为止,这是我的代码:
var MonthlyCount = from c in ClickStatistics
group c.LogDate by new { date = c.LogDate, Id = c.ID }into grp
select new{
Month = grp.Key.date.Month,
Clicks = grp.Key.Id
};
但是现在我正在得到这个:
答案 0 :(得分:1)
将具有相同c.LogDate.Month
的项目分组在一起。选择组中的第一项以将月份提取为文本值(我们按月分组,因此在这方面它们应全部相等)并计算组中的条目数。
var ClickStatistics = new List<Clicks>()
{
new Clicks() { ID = 1, LogDate = DateTime.Now.AddMonths(-1)},
new Clicks() { ID = 2, LogDate = DateTime.Now.AddMonths(-1)},
new Clicks() { ID = 3, LogDate = DateTime.Now.AddMonths(0)},
new Clicks() { ID = 4, LogDate = DateTime.Now.AddMonths(0)},
new Clicks() { ID = 5, LogDate = DateTime.Now.AddMonths(1)},
};
var MonthlyCount = from c in ClickStatistics
group c by new { date = c.LogDate.Month } into grp
select new
{
Month = grp.First().LogDate.ToString("MMMM"),
Clicks = grp.Count(),
};