即使深层复制c#

时间:2017-11-26 12:56:59

标签: c# lambda foreach

我有以下代码,其中有两个列表studentList和Student类的clonedStudentList,其中一个是其他的深层副本。我正在更新一个列表,但不应更改其他列表。但它仍在改变。 在代码中,我只是检查来自studentList的学生是否有StudentId2,通过从studentid2 ref字典中获取新值来更新clonedStudentList的StudentId值。如果学生没有studentId2值,则通过从studentid获取新值来更新clonedStudentList的StudentId值参考词典

var clonedStudentList = studentList.ToList();
clonedStudentList.ForEach(x =>
{
   studentList.ForEach(y =>
   {
     if (y.StudentId2.HasValue && StudentIds2Dictionary.ContainsKey(y.StudentId2.Value))
     {
       x.StudentId= StudentIds2Dictionary[y.StudentId2.Value];
     }
     else if(StudentIdsDictionary.ContainsKey(y.StudentId.Value))
     {
       x.StudentId= StudentIdsDictionary[y.StudentId.Value];
     }
   });
});

我也用过

var clonedStudentList = new List<Student>(studentList);

但仍然是同一个问题。

任何帮助?

0 个答案:

没有答案