在 list<DateTime> 中添加具有相同日期时间的分钟

Jon*_*Jon 0 c# linq datetime list

我有以下清单:

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

我需要一些可以以这种方式制作此列表的功能:

{ 12:00, 12:01, 12:02, 12:30, 12:31, 14:00 }
Run Code Online (Sandbox Code Playgroud)

所以,如果有相同的,DateTime我应该为每个增加 1 分钟。

Pet*_*den 5

假设times按升序排序,这应该有效:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)