C#Linq或Lambda表达式按周列出IEnumerable列表,带有日期字段

时间:2013-04-10 13:42:31

标签: c# linq lambda

我正在尝试按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表达但没有成功。

谢谢!

6 个答案:

答案 0 :(得分:13)

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);
}

答案 1 :(得分:1)

您需要定义一个可以确定日期在哪个星期的函数:

int GetWeek(DateTime date) { ... }

然后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);
    }
}

答案 2 :(得分:0)

我认为为简化此任务,您可以将Week Number属性添加到Project类中 在按周编号属性

轻松分组后,在创建项目对象时填写
Project(Name, DateStart,ID,WeekNumber)

答案 3 :(得分:0)

您可以使用GetWeekOfMonth张贴的here

然后执行以下操作:

        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);
            }
        }

答案 4 :(得分:0)

我认为只需一个简单的投影即可:

// 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++;
}

答案 5 :(得分:0)

import { Component } from '@angular/core'; import { Platform } from 'ionic-angular'; import { StatusBar } from '@ionic-native/status-bar'; import { SplashScreen } from '@ionic-native/splash-screen'; import {Storage} from '@ionic/storage'; @Component({ templateUrl: 'app.html' }) export class MyApp { rootPage:any; constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, storage: Storage) { platform.ready().then(() => { storage.ready().then(()=>{ // pretend we loaded a JWT and verified it this.rootPage = 'EventsPage'; }); splashScreen.hide(); }); } } 回答外,您还可以获取该周第一天的日期,然后按该日期分组。

获取一周中第一天的日期。 你可以使用这段代码:

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;
    }
}