C# 将正确的信息添加到新工作表

C# 将正确的信息添加到新工作表,c#,C#,我在向新列表添加数据时遇到了一点问题 这就是我的日常课程的样子 public class DailyRoutine { public DayOfWeek DayOfWeek { get; set; } public List<DateTime> G { get; set; } public List<DateTime> B { get; set; } public List<DateTim

我在向新列表添加数据时遇到了一点问题

这就是我的日常课程的样子

 public class DailyRoutine
    {
        public DayOfWeek DayOfWeek { get; set; }
        public List<DateTime> G { get; set; } 
        public List<DateTime> B { get; set; } 
        public List<DateTime> P { get; set; } 
        public List<DateTime> E { get; set; } 
    }
这是相同数据的外观:

    G   1899-12-30 07:00:00.000
    B   1899-12-30 08:30:00.000
    G   1899-12-30 11:30:00.000
    B   1899-12-30 13:30:00.000
    P   1899-12-30 17:00:00.000
    G   1899-12-30 18:00:00.000
    E   1899-12-30 19:00:00.000
这是我要填写的数据列表:

 private class Block
        {
            public DateTime Start { get; set; }
            public DateTime End { get; set; }
        }
这是我当前的代码,我用它将数据插入列表,但我想在插入之前定义,起始位置B大于第一个G,并且在块列表的末尾,包含B和G的值:

  List<Block> blocks = new List<Block>();
            for (int i = 0; i < dailyRoutine.B.Count; i++)
            {
                blocks.Add(new Block()
                {
                    Start = dailyRoutine.B[i],
                    End = dailyRoutine.G[i]
                });
            }
当前正在更改位置,我在块列表中的结果如下:

start =  B   {1899-12-30 08:30:00.000, 12/30/1899 1:30:00}
end=    G {12/30/1899 7:00:00, 12/30/1899 11:30:00 }

如果我理解正确,您希望第一个块的末尾是第二个
G
,而不是第一个,如果是这样的话,您可以通过访问大于
I
的列表索引来实现这一点。 因此:


你的问题有点不清楚,你到底有什么问题?@KieranDevlin我只是试图详细澄清一切,问题是我的数据被错误地添加到列表中,我想在列表中得到这些信息:start=B{1899-12-30 08:30:00.000,1899-12-30 13:30:00.000}end=G{1899-12-30 11:30:00.000, 1899-12-30 18:00:00.000}.for loop inserts位置对于我来说只是前两个不规则的位置。因此,如果我理解正确,您尝试添加的对象的开始日期时间的格式与您期望的不正确?如果是这种情况,日期时间本身没有格式。您在哪里看到不正确的输出?即调试窗口等?建议循环的替代方案帮助了我,它是我问题的解决方案,真的很好!我不知道我可以为这样的循环进行扩展..非常感谢!我只是有时会遇到此错误,我如何可以忽略此错误而不发生?System.ArgumentOutOfRangeException:“索引超出范围,不能为负且小于集合。参数名称:index”@Michael我已更新代码段以解决此问题。错误最有可能发生在分配
j
时,
dailroutine.B
dailroutine.G
为空。
start =  B   {1899-12-30 08:30:00.000, 1899-12-30 13:30:00.000}
end=    G {1899-12-30 11:30:00.000, 1899-12-30 18:00:00.000} 
start =  B   {1899-12-30 08:30:00.000, 12/30/1899 1:30:00}
end=    G {12/30/1899 7:00:00, 12/30/1899 11:30:00 }
List<Block> blocks = new List<Block>();
//Also need to check the index isn't going to be greater than the count of G
for (int i = 0; i < dailyRoutine.B.Count && i + 1 < dailyRoutine.G.Count; i++)
{
    blocks.Add(new Block()
    {
        Start = dailyRoutine.B[i],
        End = dailyRoutine.G[i + 1] //i + 1 will be the second instance when i is the first
    });
}
List<Block> blocks = new List<Block>();
//If either of your lists is empty, this avoids an IndexOutOfRange exception
if (dailyRoutine.B.Count > 0 && dailyRoutine.G.Count > 0)
{
    //Also need to check the index isn't going to be greater than the count of G
    for (int i = 0, j = dailyRoutine.B[0] > dailyRoutine.G[0] ? 1 : 0; i < dailyRoutine.B.Count && j < dailyRoutine.G.Count; i++, j++)
    {
        blocks.Add(new Block()
        {
            Start = dailyRoutine.B[i],
            End = dailyRoutine.G[j] //note this is 'j' not 'i'
        });
    }
}