[英]Fastest way to copy a nested list
我有一個三維清單。 外部列表表示有多少個上課時間。 下一個內部列表表示有多少學生(列表中的位置是學生的唯一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])
}
}
我不會嘗試您的類型,但是假設您的periods
集合看起來像這樣
List<List<List<int>>> p;
那么您可以復制一些內部Select
。 像這樣:
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();
不一定最快,但是使用代碼非常簡單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.