Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#按日期字段列出的组列表-每周_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C#按日期字段列出的组列表-每周

C#按日期字段列出的组列表-每周,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我有下一个实体“新闻”的列表 现在我想把它们按周分组 这是我的方法: public Dictionary<DateTime, int> GetCountByWeek(DateTime fromDate) { var vals = from l in session.Query<News>().ToList() where l.Date > fromDate group l by (l.

我有下一个实体“新闻”的列表

现在我想把它们按周分组

这是我的方法:

public Dictionary<DateTime, int> GetCountByWeek(DateTime fromDate)
{
     var vals =
            from l in session.Query<News>().ToList()
            where l.Date > fromDate
            group l by (l.Date.DayOfYear - FirstMonday(fromDate.Year)) / 7
                into gr
                select new { Key = new DateTime(gr.Min(m => m.Date.Year), gr.Min(m => m.Date.Month), gr.Min(m => m.Date.Last(DayOfWeek.Monday).Day)), Value = gr.Count() };
        return vals.ToDictionary(l => l.Key, l => l.Value);
}
这本词典应返回给我一周中第一天的日期和该周的新闻计数,如:

07.03.2016 => 15,
14.03.2016 => 7,
21.03.2016 => 8,
等等

这无法正常工作,因为它返回重复的日期键,如:

07.03.2016 => 15,
07.03.2016 => 7,
14.03.2016 => 8,
但它应该是这样的:

07.03.2016 => 15,
14.03.2016 => 7,
21.03.2016 => 8,

我对这些东西不太在行,所以如果有人能告诉我我做错了什么。

我就用日期作为分组键:

public Dictionary<DateTime, int> GetCountByWeek(DateTime fromDate)
{
     var vals =
            from l in session.Query<News>().ToList()
            where l.Date > fromDate
            group l by MyMonday(l.Date.Date) // Use Date.Date to ignore any time values
                into gr
                select new { Key = gr.Key, Value = gr.Count() };
        return vals.ToDictionary(l => l.Key, l => l.Value);
}

我从中借用了一个很好的扩展方法,然后像这样使用它:

public Dictionary<DateTime, int> GetCountByWeek(DateTime fromDate)
{
    Dictionary<string, int> dict = session.Query<News>().ToList()
        .Where(n => n.Date > fromDate)
        .GroupBy(n => n.StartOfWeek())
        .ToDictionary(g => g.Key, g => g.Count());


    return dict;
}
公共字典GetCountByWeek(DateTime fromDate)
{
Dictionary dict=session.Query().ToList()
.Where(n=>n.Date>fromDate)
.GroupBy(n=>n.StartOfWeek())
.ToDictionary(g=>g.Key,g=>g.Count());
返回命令;
}
扩展方法

public static class NewsExtensions
{
    public static DateTime StartOfWeek(this News news)
    {
        int diff = news.Date.DayOfWeek - DayOfWeek.Monday;
        if (diff < 0)
        {
            diff += 7;
        }
        return news.Date.AddDays(-1 * diff).Date;
    }
}
公共静态类新闻扩展
{
公共静态日期时间开始每周(本新闻)
{
int diff=news.Date.DayOfWeek-DayOfWeek.Monday;
如果(差异<0)
{
diff+=7;
}
返回news.Date.AddDays(-1*diff.Date);
}
}

我还没有测试过,但请试一试。

这件衣服很好用。但我还是不懂用“Date.Date”。如果您能向我解释一下,DateTime.Date实例方法将生成时间为00:00:00(12 AM)的日期。因此,以下计算结果为真:DateTime(“1/2/2016 9:00:00”)。Date==DateTime(“1/2/2016 15:00:00”)。Date。这就是你想要的分组。
public static DateTime MyMonday(DateTime date)
{
    var myMon = date;
    while (myMon.DayOfWeek != System.DayOfWeek.Monday) {
        myMon = myMon.AddDays(-1);
    }
    return myMon;
}
public Dictionary<DateTime, int> GetCountByWeek(DateTime fromDate)
{
    Dictionary<string, int> dict = session.Query<News>().ToList()
        .Where(n => n.Date > fromDate)
        .GroupBy(n => n.StartOfWeek())
        .ToDictionary(g => g.Key, g => g.Count());


    return dict;
}
public static class NewsExtensions
{
    public static DateTime StartOfWeek(this News news)
    {
        int diff = news.Date.DayOfWeek - DayOfWeek.Monday;
        if (diff < 0)
        {
            diff += 7;
        }
        return news.Date.AddDays(-1 * diff).Date;
    }
}