在特定范围内按日期分组

时间:2018-10-10 19:58:12

标签: c# .net linq group-by

假设我具有以下内存中项目和日期集合:

ItemID: 1, Date: 10/10/2018 11:30 AM
ItemID: 1, Date: 10/10/2018 11:32 AM
ItemID: 1, Date: 10/10/2018 11:33 AM
ItemID: 1, Date: 10/10/2018 11:34 AM
ItemID: 1, Date: 10/10/2018 11:57 AM
ItemID: 2, Date: 10/10/2018 7:45 AM
ItemID: 2, Date: 10/10/2018 7:49 AM
ItemID: 3, Date: 10/10/2018 8:45 AM
ItemID: 3, Date: 10/10/2018 9:13 AM

我想按ItemID和Date分组,但我希望它们的日期按最短日期的 5分钟之内的日期分组,在这种情况下,对于第1项,最短时间是:上午11:30,因此请按以下11:30 + 5分钟之间的时间分组

在上面的数据集中,将以下分组

ItemID: 1, Date: 10/10/2018 11:30 AM    <-- Grouped with Item 1
ItemID: 1, Date: 10/10/2018 11:32 AM    <-- Grouped with Item 1
ItemID: 1, Date: 10/10/2018 11:33 AM    <-- Grouped with Item 1
ItemID: 1, Date: 10/10/2018 11:34 AM    <-- Grouped with Item 1


ItemID: 1, Date: 10/10/2018 11:57 AM    <-- Not Grouped


ItemID: 2, Date: 10/10/2018 7:45 AM    <-- Grouped with Item 2
ItemID: 2, Date: 10/10/2018 7:49 AM    <-- Grouped with Item 2


ItemID: 3, Date: 10/10/2018 8:45 AM    <-- Not Grouped

ItemID: 3, Date: 10/10/2018 9:13 AM    <-- Not Grouped

分组的对象是彼此相差5分钟以内的对象。

输出:

Item 1:
Date: 10/10/2018 11:30 AM    <--- Lowest Minute is 30 so 30 + 5 range group
Date: 10/10/2018 11:32 AM
Date: 10/10/2018 11:33 AM
Date: 10/10/2018 11:34 AM

Item 1:
Date: 10/10/2018 11:57 AM

Item 2:
Date: 10/10/2018 7:45 AM
Date: 10/10/2018 7:49 AM

Item 3:
Date: 10/10/2018 8:45 AM

Item 3:
Date: 10/10/2018 9:13 AM

3 个答案:

答案 0 :(得分:5)

您可以先按ID分组,然后再按从最小分组时间起除以时间间隔后的总分钟数对每个分组进行分组,而无需提醒:

var interval = 5;
var groupedItems = items
    .GroupBy(i => i.ItemId, (k, g) => g
        .GroupBy(i => (long)(i.Date - g.Min(e => e.Date)).TotalMinutes / interval))
    .SelectMany(g => g);

答案 1 :(得分:2)

这里是一个非常粗糙的示例,我将其汇总以获取所需的“输出”。当然,将它应用于您的情况并添加您自己的错误检查/验证/订购/等等。我也没有使用LINQ,因为我不认为这是合适的per your comment。最后,我没有完全匹配您的输出。我觉得这可能是某种作业。如果是这样,那么您肯定可以进行较小的调整。

支持类别:

class Item
{
    public int ItemID { get; set; }
    public DateTime Date { get; set; }
}

示例方法:

private static void Example()
{
    var myList = new List<Item>
    {
        new Item {ItemID = 1, Date = DateTime.Parse("10/10/2018 11:30 AM") },
        new Item {ItemID = 1, Date = DateTime.Parse("10/10/2018 11:32 AM") },
        new Item {ItemID = 1, Date = DateTime.Parse("10/10/2018 11:33 AM") },
        new Item {ItemID = 1, Date = DateTime.Parse("10/10/2018 11:34 AM") },
        new Item {ItemID = 1, Date = DateTime.Parse("10/10/2018 11:57 AM") },
        new Item {ItemID = 2, Date = DateTime.Parse("10/10/2018 7:45 AM") },
        new Item {ItemID = 2, Date = DateTime.Parse("10/10/2018 7:49 AM") },
        new Item {ItemID = 3, Date = DateTime.Parse("10/10/2018 8:45 AM") },
        new Item {ItemID = 3, Date = DateTime.Parse("10/10/2018 9:13 AM") }
    };

    Item baseItem = myList.First();
    int currentGroup = 1;

    // output first group
    Console.WriteLine($"Group {currentGroup}");

    foreach (var item in myList) // .OrderBy(i=>i.ItemID).ThenBy(i => i.Date))
    {
        // if item is different OR date is >5mins from the first
        if (item.ItemID != baseItem.ItemID || item.Date > baseItem.Date.AddMinutes(5))
        {
            // different group
            currentGroup += 1;

            // set new base item
            baseItem = item;

            Console.WriteLine($"\nGroup {currentGroup}");// output new group
        }

        // output the item
        Console.WriteLine($"Item {item.ItemID}. Date: {item.Date}");
    }
}

输出:

Group 1
Item 1. Date: 10/10/2018 11:30:00 AM
Item 1. Date: 10/10/2018 11:32:00 AM
Item 1. Date: 10/10/2018 11:33:00 AM
Item 1. Date: 10/10/2018 11:34:00 AM

Group 2
Item 1. Date: 10/10/2018 11:57:00 AM

Group 3
Item 2. Date: 10/10/2018 7:45:00 AM
Item 2. Date: 10/10/2018 7:49:00 AM

Group 4
Item 3. Date: 10/10/2018 8:45:00 AM

Group 5
Item 3. Date: 10/10/2018 9:13:00 AM

答案 2 :(得分:2)

这里有一个功能齐全的工作示例,说明如何解决此问题,并与前两个答案保持了一致。只需打开VS并创建一个新的ConsoleApp并将其粘贴并尝试即可。希望您现在能找到解决方法。

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp3
{
    public class Item
    {
        public int ItemID { get; set; }
        public DateTime Date { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Item> items = new List<Item>
            {
                new Item { ItemID= 1, Date = new DateTime(2018, 10, 10, 11, 30, 0) },
                new Item { ItemID= 1, Date = new DateTime(2018, 10, 10, 11, 32, 0) },
                new Item { ItemID= 1, Date = new DateTime(2018, 10, 10, 11, 33, 0) },
                new Item { ItemID= 1, Date = new DateTime(2018, 10, 10, 11, 34, 0) },
                new Item { ItemID= 1, Date = new DateTime(2018, 10, 10, 11, 57, 0) },
                new Item { ItemID= 2, Date = new DateTime(2018, 10, 10, 7, 45, 0) },
                new Item { ItemID= 2, Date = new DateTime(2018, 10, 10, 7, 49, 0) },
                new Item { ItemID= 2, Date = new DateTime(2018, 10, 10, 8, 45, 0) },
                new Item { ItemID= 2, Date = new DateTime(2018, 10, 10, 9, 13, 0) }
            };

            var groupedItems = items.GroupBy(i => i.ItemID, (k, g) => g
          .GroupBy(i => (long)new TimeSpan(i.Date.Ticks - g.Min(e => e.Date).Ticks).TotalMinutes / 5));

            var groupIndex = 0;
            foreach (var group in groupedItems)
            {
                foreach (IGrouping<long, Item> groupItem in group)
                {
                    Console.Out.WriteLine("Group " + (++groupIndex));

                    foreach (var item in groupItem)
                    {
                        Console.WriteLine($"Item {item.ItemID}. Date: {item.Date}");

                    }
                    Console.Out.WriteLine();
                }
            }
            Console.ReadLine();
        }
    }
}

欢呼声