c# Linq或Lambda表达式按周分组IEnumerable列表与日期字段

本文关键字:IEnumerable 列表 字段 日期 Linq Lambda 表达式 | 更新日期: 2023-09-27 18:08:22

我正试图按周对IEnumerable<Object>进行查询,例如:

Project(Name, DateStart,ID)

我有IEnumerable<Project>,我想做一个报告,按周分组。

例子:

Week 1
 Project1  8/4/2013 ID1
 Project2  9/4/2013 ID2
Week 2
 Project1  16/4/2013 ID3
 Project2  18/4/2013 ID5
Week 3
 Project1  24/4/2013 ID7
 Project2  26/4/2013 ID8
如果有人能帮我一把,我真的很感激!我试图做一个lambda表达式,但没有成功。

谢谢!

c# Linq或Lambda表达式按周分组IEnumerable列表与日期字段

var weekGroups = projects
    .Select(p => new 
    { 
        Project = p, 
        Year = p.DateStart.Year, 
        Week =  CultureInfo.InvariantCulture.Calendar.GetWeekOfYear
                      (p.DateStart, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)
    })
    .GroupBy(x => new { x.Year, x.Week })
    .Select((g, i) => new 
    { 
        WeekGroup = g, 
        WeekNum = i + 1,
        Year = g.Key.Year,
        CalendarWeek = g.Key.Week
    });
foreach (var projGroup in weekGroups)
{
    Console.WriteLine("Week " + projGroup.WeekNum);
    foreach(var proj in projGroup.WeekGroup)
        Console.WriteLine("{0} {1} {2}", 
            proj.Project.Name, 
            proj.Project.DateStart.ToString("d"), 
            proj.Project.ID);
}

您需要定义一个函数来确定日期是在哪一周:

int GetWeek(DateTime date) { ... }

则对group的LINQ查询为:

IEnumerable<IGrouping<int, Project>> groupedProjects = 
         myListOfProjects.GroupBy(p => GetWeek(p.DateStart));

您可以遍历分组项目列表:

foreach (var weekGroup in groupedProjects)
{
    int week = weekGroup.Key;
    foreach (var project in weekGroup)
    {
        Console.WriteLine("Week " + week + " | Project: " + project.ID);
    }
}

为了简化这个任务,您可以将WeekNumber属性添加到Project类中并填充它,当你创建项目对象后,你可以很容易地按WeekNumber属性分组

Project(Name, DateStart,ID,WeekNumber)

我想一个简单的投影就足够了:

// we'll just use the existing settings on the box.
DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
var cal = dfi.Calendar;
var Projects = GetProjectsFromSomewhere(); // assuming IEnumerable<Project>
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++;
}

您可以使用GetWeekOfMonth扩展张贴在这里:

,然后执行如下操作:

        var groups = projects.GroupBy(p => p.Start.GetWeekOfMonth());
        foreach (var group in groups)
        {
            Console.WriteLine("Week {0}", group.Key);
            foreach (var project in group)
            {
                Console.WriteLine("'t{0} | {1:dd/MM/yyyy} | {2}", 
                     project.Name, project.Start, project.Id);
            }
        }

除了 Tim Schmelter's 答案之外,您还可以获得一周第一天的日期,然后按该日期分组。

获取一周第一天的日期。您可以使用以下代码:

public static class DateTimeExtensions
{
    public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
    {
        int diff = dt.DayOfWeek - startOfWeek;
        if (diff < 0)
        {
            diff += 7;
        }
        return dt.AddDays(-1 * diff).Date;
    }
}

那么你可以按一周的第一个日期分组,像这样:

var weekGroups = projects
    .Select(p => new 
    { 
        Project = p
    })
    .GroupBy(x => x.Project.DateStart.StartOfWeek(DayOfWeek.Monday))
    .Select((g, i) => new 
    { 
        WeekGroup = g, 
        WeekNum = i + 1
    });
foreach (var projGroup in weekGroups)
{
    Console.WriteLine("Week " + projGroup.WeekNum);
    foreach(var proj in projGroup.WeekGroup)
        Console.WriteLine("{0} {1} {2}", 
            proj.Project.Name, 
            proj.Project.DateStart.ToString("d"), 
            proj.Project.ID);
}