I have a list of entries which is saved into database by following - shortened - code:
List<MyStruct> myStructList = new JavaScriptSerializer().Deserialize<List<MyStruct>>(postdata);
foreach (MyStruct myStruct in myStructList) {
if(myStruct.id==0) {
// Not in DB -> insert.
myStruct.id = (int)db.ExecuteScalar("INSERT INTO table ...");
} else {
...
}
}
// return all records with id set to auto-increment value.
return myStructList;
I want to return all records with updated id - but myStruct.id
is not writeable, because of the foreach. So I replaced the foreach by a for loop:
for(int i=0;i<myStructList.Count;i++) //foreach (MyStruct myStruct in myStructList), but writeable
{
MyStruct myStruct = myStructList[i]
if(myStruct.id==0) {
// Not in DB -> insert.
myStruct.id = (int)db.ExecuteScalar("INSERT INTO table ...");
}
}
return myStructList;
but changing myStruct does not change myStructList.
Third try: Write back into the List.
for(int i=0;i<myStructList.Count;i++) //foreach (MyStruct myStruct in myStructList), but writeable
{
MyStruct myStruct = myStructList[i]
if(myStruct.id==0) {
// Not in DB -> insert.
myStructList[i].id = (int)db.ExecuteScalar("INSERT INTO table ...");
}
}
return myStructList;
which returns the error:
Cannot modify the return value of 'System.Collections.Generic.List<MyApp.MyStruct>.this[int]' because it is not a variable.
So how on earth can I get this done?