Can someone tell me why the following code is n开发者_开发问答ot updating StudentIDs in LINQPad?
var studentsList = Students.Select(t => t);
int newStudentID = 10001;
foreach(Student s in studentsList)
{
s.StudentID = newStudentID;
newStudentID = newStudentID + 1;
}
SubmitChanges();
Thank you!
not sure; i tried to write code to test this using the snippet you gave but that would compile under LinqPad; here's what i used:
public class Student
{
public string name;
public int StudentId;
}
void Main()
{
Student[] Students =
new Student[]
{
new Student { name="a", StudentId = 0},
new Student { name="b", StudentId = 0},
new Student { name="c", StudentId = 0},
new Student { name="d", StudentId = 0}
};
var studentsList = Students.Select(t => t);
int newStudentID = 10001;
foreach(var s in studentsList)
{
s.StudentId = newStudentID;
newStudentID = newStudentID + 1;
}
foreach(var s in studentsList)
{
System.Console.WriteLine(s.name + ": " + s.StudentId.ToString());
}
}
and here was the output:
a: 10001
b: 10002
c: 10003
d: 10004
so it looks like that much worked ok. if you can give enough context to have a running example of your code i'd be happy to check it out further.
精彩评论