Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在列表中有相同日期时间的位置添加分钟<;日期时间>;_C#_List_Linq_Datetime - Fatal编程技术网

C# 在列表中有相同日期时间的位置添加分钟<;日期时间>;

C# 在列表中有相同日期时间的位置添加分钟<;日期时间>;,c#,list,linq,datetime,C#,List,Linq,Datetime,我有以下清单: List<DateTime> list = new [] { "12:00", "12:00", "12:00", "12:30", "12:30", "14:00" } .Select(DateTime.Parse) .ToList(); 因此,如果有相同的日期时间我应该为每个时间增加1分钟。您可以使用LI

我有以下清单:

List<DateTime> list =
    new [] { "12:00", "12:00", "12:00", "12:30", "12:30", "14:00" }
        .Select(DateTime.Parse)
        .ToList();

因此,如果有相同的
日期时间
我应该为每个时间增加1分钟。

您可以使用LINQ并这样做(假设您的时间戳类型为
日期时间

公共静态IEnumerable MakeUnique(IEnumerable时间戳)
{
返回timestamps.GroupBy(t=>t).SelectMany(g=>g.Select((t,i)=>t.AddMinutes(i));
}

它首先将时间戳分组为具有相同时间戳的组。然后,对于每个组,它将组视为一个列表,并添加x分钟数,其中x是列表中的索引。第一个加0分钟,第二个加1分钟,依此类推。然后,它使用SelectMany将较小的列表展平为单个列表。

假设
时间是按升序排序的,这应该可以工作:

private static IEnumerable<DateTime> NewTimes(IEnumerable<DateTime> times)
{
    var current = DateTime.MinValue;
    foreach (var time in times)
    {
        if (time > current) current = time;
        yield return current;
        current = current.AddMinutes(1);
    }
}
私有静态IEnumerable NewTimes(IEnumerable times)
{
var current=DateTime.MinValue;
foreach(以时间为单位的var时间)
{
如果(时间>电流)电流=时间;
产生回流电流;
当前=当前添加分钟数(1);
}
}

这项工作通过
完成。聚合

List<DateTime> list =
    new [] { "12:00", "12:00", "12:00", "12:30", "12:30", "14:00" }
        .Select(DateTime.Parse)
        .ToList();

List<DateTime> output =
    list.Skip(1).Aggregate(list.Take(1).ToList(), (a, x) =>
    {
        a.Add(a.Last() >= x ? a.Last().AddMinutes(1.0): x);
        return a;
    });
如果您很乐意使用“System.Interactive”来获取可枚举项的
Scan
扩展方法,那么这也适用于:

IEnumerable<DateTime> output =
    list
        .Scan((a, x) => x > a ? x : a.AddMinutes(1.0))
        .StartWith(list.First());
IEnumerable输出=
列表
.扫描((a,x)=>x>a?x:a.AddMinutes(1.0))
.StartWith(list.First());

您需要某种循环,并将最后找到的副本保存在某个地方。到目前为止,您尝试过什么?您的列表是否总是按升序排序?如果不是,如果您的值是
{12:00,12:01,12:00}
,结果会是什么?第二个
12:00
是否变成
12:01
12:02
?我的列表总是按升序排序
{12:00,12:00,12:01}
的输出应该是什么?{12:00,12:01,12:02}这会导致类似12:00,12:00的结果吗。。。30次,然后12:30生产12:00,12:01…12:29,12:30,12:30?是的。只要一组中的结果不与另一组中的结果重叠,它就可以工作。所以它适用于OP的原始示例,但不适用于评论中的某些示例。它不适用于12:00、12:00、12:01之类的情况。然后它将返回12:00,12:01,12:01,因为它将分为两组,12:01将在两组的输出中。是的,你是对的,你对这种情况有什么建议吗?为了解决这个问题,我用与PeterYeah相同的方法来做这看起来是对的(考虑到所有新信息)+1
List<DateTime> list =
    new [] { "12:00", "12:00", "12:00", "12:30", "12:30", "14:00" }
        .Select(DateTime.Parse)
        .ToList();

List<DateTime> output =
    list.Skip(1).Aggregate(list.Take(1).ToList(), (a, x) =>
    {
        a.Add(a.Last() >= x ? a.Last().AddMinutes(1.0): x);
        return a;
    });
{ 12:00, 12:01, 12:02, 12:30, 12:31, 14:00 }
IEnumerable<DateTime> output =
    list
        .Scan((a, x) => x > a ? x : a.AddMinutes(1.0))
        .StartWith(list.First());