0

我有一个三维列表。外部列表表示有多少个课时。下一个内部列表表示有多少学生(列表中的位置是学生的唯一 ID)。最终列表是学生在该期间学习的课程 ID。

我可以复制此列表的最快方法是什么?

我试过了

a.var = var.Select(x => x.ToList()).ToList().ToList();

但这不起作用。以下是我正在使用的,但我相信有一种更清洁、更快的方法,我想学习如何。

foreach (PERIOD period in periods)
{
  a.var.Add(new List<List<int>>());

  for (int student = 0; student < students.Count + 1; student++)
     a.var[IntFromEnum(period)].Add(new List<int>());

  foreach (Course course in periods[IntFromEnum(period)])
  {
     foreach (int student in course.students)
       a.var[IntFromEnum(period)][student] = new List<int>(var[IntFromEnum(period)][student])
  }
}
4

1 回答 1

1

我不会尝试你的类型,但假设你的periods收藏看起来像这样

List<List<List<int>>> p;

然后你可以做一些 inner Selects 来复制。像这样的东西:

var a = p.Select(x => new List<List<int>>(x.Select(y => new List<int>(y)))).ToList();

或清洁工:

var a = p.Select(x => x.Select(y => y.ToList()).ToList()).ToList();

不一定是最快的,但代码非常简单。

于 2013-05-31T18:50:04.023 回答