LINQ根据时间戳对对象进行分组

发布于 2024-08-26 15:27:30 字数 385 浏览 4 评论 0原文

我有一系列对象定义为:

public class Foo
{
           public DateTime Time {get;set;}
}

现在我想根据时间对对象(IEnumerable)进行分组,例如我想根据小时或天或月对它们进行分组< /强>。

例如(按小时分组):

group 1(13:00-14:00) : foo1, foo2, foo3
group 2(14:00-15:00): foo4, foo5

如何在此基础上编写 LINQ?希望我说清楚了。

I have a serial of object defined as:

public class Foo
{
           public DateTime Time {get;set;}
}

now I want to group objects(IEnumerable<Foo>) according to the time, e.g. I want to group them according to hour or day or month.

for example (group into hour):

group 1(13:00-14:00) : foo1, foo2, foo3
group 2(14:00-15:00): foo4, foo5

How to write LINQ over this? hope I made myself clear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

意中人 2024-09-02 15:27:30

Hour 是 DateTime 的一个属性,因此它应该像这样简单:

fooList.GroupBy(f => f.Hour)

这将为您提供一个 IGrouping 对象,其中 int 键是小时。

Hour is a property of DateTime, so it should be as simple as:

fooList.GroupBy(f => f.Hour)

That would give you an IGrouping<int, Foo> object, in which the int key is the hour.

却一份温柔 2024-09-02 15:27:30

这是使用查询表达式的示例。

public class Foo
{
    public int FooID { get; set; }
    public DateTime FooDate { get; set; }
}

...

List<Foo> foos = new List<Foo>()
{
    new Foo() { FooID = 1, FooDate = new DateTime(2010,3,15,18,30,0)},
    new Foo() { FooID = 2, FooDate = new DateTime(2010,3,15,19,30,0)},
    new Foo() { FooID = 3, FooDate = new DateTime(2010,3,15,20,30,0)},
    new Foo() { FooID = 4, FooDate = new DateTime(2010,3,15,18,15,0)},
    new Foo() { FooID = 5, FooDate = new DateTime(2010,3,15,18,45,0)},
    new Foo() { FooID = 6, FooDate = new DateTime(2010,3,15,20,15,0)},
    new Foo() { FooID = 7, FooDate = new DateTime(2010,3,15,19,15,0)}
};

var query = from foo in foos
            group foo by foo.FooDate.Hour
                into foogroup
                select new
                {
                    Hour = foogroup.Key,
                    Foos = foos.Where(foo => foo.FooDate.Hour == foogroup.Key)
                };

foreach (var group in query)
{
    Console.WriteLine(group.Hour);
    foreach (Foo foo in group.Foos)
        Console.WriteLine("\t{0}\t{1}", foo.FooID, foo.FooDate);
}

编辑:关于需要分隔日、月等,您可以像下面这样扩展分组

var query = from foo in foos
            group foo by new { Date = foo.FooDate.Date, Hour = foo.FooDate.Hour }
                into foogroup
                select new
                {
                    Date = foogroup.Key.Date,
                    Hour = foogroup.Key.Hour,
                    Foos = foos.Where(foo => 
                        foo.FooDate.Date == foogroup.Key.Date 
                        && foo.FooDate.Hour == foogroup.Key.Hour)
                };

Here's an example using query expression.

public class Foo
{
    public int FooID { get; set; }
    public DateTime FooDate { get; set; }
}

...

List<Foo> foos = new List<Foo>()
{
    new Foo() { FooID = 1, FooDate = new DateTime(2010,3,15,18,30,0)},
    new Foo() { FooID = 2, FooDate = new DateTime(2010,3,15,19,30,0)},
    new Foo() { FooID = 3, FooDate = new DateTime(2010,3,15,20,30,0)},
    new Foo() { FooID = 4, FooDate = new DateTime(2010,3,15,18,15,0)},
    new Foo() { FooID = 5, FooDate = new DateTime(2010,3,15,18,45,0)},
    new Foo() { FooID = 6, FooDate = new DateTime(2010,3,15,20,15,0)},
    new Foo() { FooID = 7, FooDate = new DateTime(2010,3,15,19,15,0)}
};

var query = from foo in foos
            group foo by foo.FooDate.Hour
                into foogroup
                select new
                {
                    Hour = foogroup.Key,
                    Foos = foos.Where(foo => foo.FooDate.Hour == foogroup.Key)
                };

foreach (var group in query)
{
    Console.WriteLine(group.Hour);
    foreach (Foo foo in group.Foos)
        Console.WriteLine("\t{0}\t{1}", foo.FooID, foo.FooDate);
}

Edit: Regarding the need to seperate days, months, etc., you can extend your grouping like the following

var query = from foo in foos
            group foo by new { Date = foo.FooDate.Date, Hour = foo.FooDate.Hour }
                into foogroup
                select new
                {
                    Date = foogroup.Key.Date,
                    Hour = foogroup.Key.Hour,
                    Foos = foos.Where(foo => 
                        foo.FooDate.Date == foogroup.Key.Date 
                        && foo.FooDate.Hour == foogroup.Key.Hour)
                };
浊酒尽余欢 2024-09-02 15:27:30

如果您不想将昨天的 13-14 小时与今天的 13-14 小时分组,也不想将本月 5 日与上个月 5 日等分组,那么您需要执行类似的操作:-

// splitting time up into hour intervals
fooList.GroupBy(f => f.Time.Date.AddHours(f.Time.Hour))

// splitting time up into day intervals
fooList.GroupBy(f => f.Time.Date)

// splitting time up into month intervals
fooList.GroupBy(f => f.Time.Date.AddDays(-f.Time.Day))

如果您确实想将所有相同的小时/天/月映射到同一组中,那么会更容易。

If you don't want to group the 13-14hr yesterday with the 13-14hr today, nor the 5th of this month with the 5th of last month etc. so you'll need to do something like:-

// splitting time up into hour intervals
fooList.GroupBy(f => f.Time.Date.AddHours(f.Time.Hour))

// splitting time up into day intervals
fooList.GroupBy(f => f.Time.Date)

// splitting time up into month intervals
fooList.GroupBy(f => f.Time.Date.AddDays(-f.Time.Day))

If you really do want to map all of the same hour/day/month into the same group then it's easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文