Why can't I replace each object's property in an IEnumerble list within loop?
foreach(var group in groupedClassOptions){
var gradeNo = group.GroupName.Split(' ')[1];
if (gradeNo != grade0_name)
{
var groupTotal = temp.Where(t => t.grade.ToString().Equals(gradeNo)).First().total;
group.GroupName += " (" + groupTotal + " students)";
}
else
{
var prepGroupTotal = temp.Where(t => t.grade.ToString().Equals("0")).First().total;
group.GroupName += " (" + prepGroupTotal + " students)";
}
}
after i exit this loop the property GroupName of each group element was expected to change but it didn't.
Example data - groupedClassOptions
[GroupName: Grade Prep,
Options : [Prep A (23 students), Prep B (21 students)],
GroupName: Grade 1,
Options : [1 A (23 students), 1 B (21 students)],
GroupName: Grade 2,
Options : [2 A (23 students), 2 B (21 students)],
]
This is just so you can get the idea, pls ignore any syntax errors. Now i want to replace GroupName: Grade 1 with GroupName: Grade 1 (x students)
Implementation of groupedClassOptions
var groupedClassOptions = dataClass.GroupBy(x => x.grade).Select(x => new OptionGroupVM()
{
GroupName = "Grade " + (x.Key == 0 ? grade0_name : x.Key.ToString()) /*+ " (" + (groupTotal.Where(t => t.grade.ToString().Equals(x.Key)).First().total) + " students)"*/,
Options = x.Select(y => new OptionVM()
{
Value = y.classID.ToString(),
Text = x.Key == 0 ? grade0_name + y.classname.Substring(1) + " (" + y.total + " students)" : y.classname + " (" + y.total + " students)"
})
});