C# Linq or Lambda expression Group by Week IEnumerable list with date field

后端 未结 6 1425
梦如初夏
梦如初夏 2021-01-01 04:53

I am trying to do a query to an IEnumerable to group by week, for example:

Project(Name, DateStart,ID)

I have

6条回答
  •  情歌与酒
    2021-01-01 05:14

    I think just a simple projection will suffice:

    // we'll just use the existing settings on the box.
    DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
    var cal = dfi.Calendar;
    
    var Projects = GetProjectsFromSomewhere(); // assuming IEnumerable
    
    int week = 1; // this *could* be changed and projected in the Select instead.
    foreach (var wg in from p in Projects
        group p by cal.GetWeekOfYear(
            p.StartDate, dfi.CalendarWeekRule, dfi.FirstDayOfWeek))
    {
        Console.WriteLine("Week {0}", week);
    
        foreach (var p in wg)
        {
            Console.WriteLine("    {0} {1} {2}", p.Name, p.StartDate, p.ID);
        }
        week++;
    }
    

提交回复
热议问题