寻找C#算法从一个DateTime对象的集合抓取重复项目(考虑日期和时间)? [英] Looking for C# algorithm to grab recurring items from a collection of DateTime objects (factoring in both date and time)?

查看:261
本文介绍了寻找C#算法从一个DateTime对象的集合抓取重复项目(考虑日期和时间)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DateTime 对象的集合。我需要从这个原始列表中解析出子集,以基于重复获取相关项目。所以我需要采取这个单一的原始集合:

  var collectionOfDateTime = GetDateTimeCollection(); 

并将其转换为 DateTime



在下面的例子中,我不包括时间,而是包括时间。实际需求,这些项目都有日期和时间元素。例如,日期需要为7天的时间,但也是相同的时间(2月3日上午11点的一次不匹配feb 10th在下午3点,但它匹配2月10日上午11点)



例如,假设我的循环模式是每周(在其他情况下它可能是每月),我的日期集合看起来像这样:

  var date1 = DateTime.Today.AddHours(8); 
var date2 = DateTime.Today.AddWeeks(1).AddHours(8);
var date3 = DateTime.Today.AddDays(3);
var date4 = DateTime.Today.AddWeeks(8).AddHours(6);

var collectionOfDateTime = new List< DateTime>(){date1,date2,date3,date4};

我想要一个函数(让它调用 StripOutSubCollections c $ c>)传递 collectionOfDateTime 以及它的Weekly并返回一个包含 date1 date2 (因为它们都是同一个每周时段的一部分)。注意date3不适合,date4也不适合,因为小时与其他小时不匹配



另一个例子来帮助证明需求,如果对原始集合的输入是:

  var date1 = DateTime.Today; 
var date2 = DateTime.Today.AddWeeks(1);
var date3 = DateTime.Today.AddDays(3);
var date4 = DateTime.Today.AddWeeks(8);
var date5 = DateTime.Today.AddDays(3).AddWeeks(2);

var collectionOfDateTime = new List< DateTime>(){date1,date2,date3,date4,date5};

我想让这个函数返回两个列表(一个列表 date1 date2 date4 )和另一个列表 date3 date5



如果我需要更多的例子来说明要求,请注意,其中一个日期可能属于多个输出列表,这是可以的。



我可以将每周转换为数字7,并对每个项目开始循环第一个项目到结束,然后第二个项目到结束,然后第三个项目,等,但想看看是否有一个更优雅的方式

解决方案

如果我正确理解你的问题,你是否试图按照每周的日子来分块 DateTime



如果是这样,这样的东西应该这样:

  var byDayOfWeek = collectionOfDateTime.GroupBy dt = dt.DayOfWeek); 
//根据星期几选择转换为字典
var asDict = byDayOfWeek.ToDictionary(grp => grp.Key,grp => grp.ToList());

foreach(var kvp in asDict)
{
Console.WriteLine(Day:+ kvp.Key);
foreach(var value in kvp.Value)
{
Console.WriteLine(value);
}
}

输出:

 日期:星期四
2/21/2013 12:00:00 AM
2/28/2013 12:00:00 AM
4/18/2013 12:00:00 AM
日期:星期日
2/24/2013 12:00:00 AM

编辑:对于多个按分组规则:

  ChunkType 
{
每周,
每月,
每年
}

public IEnumerable< IEnumerable< DateTime> ChunkDates(IEnumerable< DateTime> collection,ChunkType chunkBy)
{
switch(chunkBy)
{
case ChunkType.Weekly:
//大致等于星期几
return collection.GroupBy(dt => dt.DayOfWeek).Select(grp => grp.ToList());
case ChunkType.Monthly:
// Trickier - 假定按月的顺序日?
return collection.GroupBy(dt => dt.Day).Select(grp => grp.ToList());
case ChunkType.Yearly:
// Trickier - 假设为一年中的有序日?
return collection.GroupBy(dt => dt.DayOfYear).Select(grp => grp.ToList());
}
return new [] {collection};
}

var date1 = DateTime.Today;
var date2 = DateTime.Today.AddDays(7);
var date3 = DateTime.Today.AddDays(3);
var date4 = DateTime.Today.AddDays(8 * 7);

var collectionOfDateTime = new List< DateTime>(){date1,date2,date3,date4};

foreach(var type in new [] {ChunkType.Weekly,ChunkType.Monthly,ChunkType.Yearly})
{
Console.WriteLine(Now grouping by:+类型);
var grouped = ChunkDates(collectionOfDateTime,type);
foreach(var groupOfDates in grouped)
{
Console.WriteLine(New group!);
foreach(groupOfDates中的var值)
{
Console.WriteLine(value);
}
}
}

输出:

 现在分组:每周
新组!
2/21/2013 12:00:00 AM
2/28/2013 12:00:00 AM
4/18/2013 12:00:00 AM
新组!
2/24/2013 12:00:00 AM

现在分组:每月
新组!
2/21/2013 12:00:00 AM
新组!
2/28/2013 12:00:00 AM
新组!
2/24/2013 12:00:00 AM
新组!
4/18/2013 12:00:00 AM

现在分组:每年
新组!
2/21/2013 12:00:00 AM
新组!
2/28/2013 12:00:00 AM
新组!
2/24/2013 12:00:00 AM
新组!
4/18/2013 12:00:00 AM


I have a collection of DateTime objects. I need to "parse" out subcollections from this original list to grab related items based on recurrence. So I need to take this single original collection:

var collectionOfDateTime = GetDateTimeCollection();

and translate that into a list of DateTime collections where each collection includes a set of dates from the first list that following a specific recurrence pattern.

In my examples below I am not including time but in the real requirement, these items have both Date and Time elements to them. So for example the Dates need to be 7 days apart but also be the same time (one time on Feb 3rd at 11AM doesn't match feb 10th at 3PM but it does match with Feb 10th at 11AM)

For example, lets say my recurrence pattern is "Weekly" (in other cases it might be "Monthly") and my date collection looks like this:

 var date1 = DateTime.Today.AddHours(8);
 var date2 = DateTime.Today.AddWeeks(1).AddHours(8);
 var date3 = DateTime.Today.AddDays(3);
 var date4 = DateTime.Today.AddWeeks(8).AddHours(6);

 var collectionOfDateTime = new List<DateTime>() { date1, date2, date3, date4 };

I would want a function (lets call it StripOutSubCollections()) to pass in the collectionOfDateTime and the fact that its "Weekly" and return one collection that includes date1, date2 (as they are all part of the same weekly slot). Note date3 doesn't not fit and date4 also doesn't fit because the hours do not match with the others

For another example to help prove out the requirement, if the input to the original collection was this:

 var date1 = DateTime.Today;
 var date2 = DateTime.Today.AddWeeks(1);
 var date3 = DateTime.Today.AddDays(3);
 var date4 = DateTime.Today.AddWeeks(8);
 var date5 = DateTime.Today.AddDays(3).AddWeeks(2);

 var collectionOfDateTime = new List<DateTime>() { date1, date2, date3, date4, date5 };

I would want this function to return 2 lists (one list with date1, date2, and date4) and another list with date3 and date5.

Let me know if I need more examples to articulate the requirements? Note that is possible that one of the dates might fall into multiple output lists which is fine.

I can convert weekly into the number 7 and do a loop through each items starting with first item to the end, then the second item to the end, then the third item, etc. but wanted to see if there was a more elegant way

解决方案

If I understand your question properly, you are trying to "chunk" the DateTime values by Day of the Week?

If so, something like this should do it:

var byDayOfWeek = collectionOfDateTime.GroupBy(dt => dt.DayOfWeek);
// Optionally convert to a dictionary by day of week
var asDict = byDayOfWeek.ToDictionary(grp => grp.Key, grp => grp.ToList());

foreach(var kvp in asDict)
{
    Console.WriteLine("Day:" + kvp.Key);
    foreach (var value in kvp.Value)
    {
        Console.WriteLine(value);
    }
}

Output:

Day:Thursday
2/21/2013 12:00:00 AM
2/28/2013 12:00:00 AM
4/18/2013 12:00:00 AM
Day:Sunday
2/24/2013 12:00:00 AM

EDIT: For multiple "grouping by" rules:

public enum ChunkType
{
    Weekly,
    Monthly,
    Yearly
}

public IEnumerable<IEnumerable<DateTime>> ChunkDates(IEnumerable<DateTime> collection, ChunkType chunkBy)
{
    switch(chunkBy)
    {
        case ChunkType.Weekly:
            // roughly equals by day of week
            return collection.GroupBy(dt => dt.DayOfWeek).Select(grp => grp.ToList());
        case ChunkType.Monthly:
            // Trickier - assume by ordinal day of month?
            return collection.GroupBy(dt => dt.Day).Select(grp => grp.ToList());
        case ChunkType.Yearly:
            // Trickier - assume by ordinal day of year?
            return collection.GroupBy(dt => dt.DayOfYear).Select(grp => grp.ToList());        
    }
    return new[]{ collection };
}

var date1 = DateTime.Today;
var date2 = DateTime.Today.AddDays(7);
var date3 = DateTime.Today.AddDays(3);
var date4 = DateTime.Today.AddDays(8*7);

var collectionOfDateTime = new List<DateTime>() { date1, date2, date3, date4};

foreach(var type in new [] { ChunkType.Weekly, ChunkType.Monthly, ChunkType.Yearly })
{
    Console.WriteLine("Now grouping by:" + type);
    var grouped = ChunkDates(collectionOfDateTime, type);
    foreach(var groupOfDates in grouped)
    {
        Console.WriteLine("New group!");
        foreach (var value in groupOfDates)
        {
            Console.WriteLine(value);
        }
    }
}

Output:

Now grouping by:Weekly
New group!
2/21/2013 12:00:00 AM
2/28/2013 12:00:00 AM
4/18/2013 12:00:00 AM
New group!
2/24/2013 12:00:00 AM

Now grouping by:Monthly
New group!
2/21/2013 12:00:00 AM
New group!
2/28/2013 12:00:00 AM
New group!
2/24/2013 12:00:00 AM
New group!
4/18/2013 12:00:00 AM

Now grouping by:Yearly
New group!
2/21/2013 12:00:00 AM
New group!
2/28/2013 12:00:00 AM
New group!
2/24/2013 12:00:00 AM
New group!
4/18/2013 12:00:00 AM

这篇关于寻找C#算法从一个DateTime对象的集合抓取重复项目(考虑日期和时间)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆