开发者

Update Table using LINQ

开发者 https://www.devze.com 2023-03-19 18:52 出处:网络
Can someone tell me why the following code is n开发者_开发问答ot updating StudentIDs in LINQPad?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号