计算每周点击次数的统计信息

时间:2018-08-08 10:46:19

标签: c# graph statistics

所以我有一个小应用程序,可以注册某些语句的点击次数。

我有一个名为ClickStatistics的表 其中存储了所单击的特定语句的ID,以及一个日期时间“ LogDate”。

我想创建一个图表,告诉我每周有多少点击。

我用几个月的时间做了类似的事情,但是我无法在数周内想出办法。

var months = new[]{
                "Januar",
                "Februar",
                "Marts",
                "April",
                "Maj",
                "Juni",
                "Juli",
                "August",
                "September",
                "Oktober",
                "November",
                "December"
            };
var MonthlyCountQuery = from c in ClickStatistics
                                    where c.LogDate.Year == selectedYear && c.Statement.Deleted == false
                                    group c by c.LogDate.Month into grp
                                    select new
                                    {
                                        Month = grp.Key,
                                        Clicks = grp.Count(),
                                    };

            var monthlyCount = MonthlyCountQuery.ToList().Select(i => new { Month = months[i.Month - 1], i.Clicks});

欢迎任何提示!

2 个答案:

答案 0 :(得分:1)

您可以扩展DateTime,然后按该方法分组。

我使用的扩展名(不太确定,但我认为我在SO上找到了它)

 公共静态整数? GetIso8601WeekOfYear(this DateTime?dt)
{
    如果(!dt.HasValue)返回null;

    //认真作弊。如果是星期一,星期二或星期三,它将
    //与星期四,星期五或星期六是同一周#,
    //并且我们总是正确的
    DayOfWeek天= CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(dt.Value);
    如果(day> = DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
    {
        dt = dt.Value.AddDays(3);
    }

    //返回调整后日期的星期
    返回CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(dt.Value,CalendarWeekRule.FirstFourDayWeek,DayOfWeek.Monday);
}
 

但是请注意,就像上面评论中提到的Stefan一样,您需要检查所需的标准。您可能会编写其他扩展方法。

Update :扩展方法来自于此帖子: 获取给定日期的正确星期数

请为@il_guru投票;)

答案 1 :(得分:1)

所以,像这样:

DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
Calendar cal = dfi.Calendar;

//just to make formatting on SO better
var whereClause = ClickStatistics
         .Where(i => i.LogDate.Year == selectedYear && i.Statement.Deleted == false).ToList();

var WeekCountQuery = from c in whereClause
                     group c by 
                     cal.GetWeekOfYear(c.LogDate, dfi.CalendarWeekRule, dfi.FirstDayOfWeek) 
                     into grp
                     select new
                     {
                          Week = grp.Key,
                          Clicks = grp.Count(),
                     };

或者当然是注释中提到的其他扩展方法之一。

例如:

var WeekCountQuery = from c in whereClause
                     group c by 
                     //make sure you have the extension method
                     c.LogDate.GetIso8601WeekOfYear() 
                     into grp
                     select new
                     {
                          Week = grp.Key,
                          Clicks = grp.Count(),
                     };