1

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)"
            })
        });
Samra
  • 1,815
  • 4
  • 35
  • 71

1 Answers1

2

You need to convert groupedClassOptions to list using ToList() method which makes your object inside list to be updatable.

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)"
            })
        }).ToList();

Now, update object inside above list.

The reason behind not updating object inside IEnumerable is posted in following answer:

IEnumerables do not guarantee that updated values will persist across enumerations. For instance, a List will return the same set of objects on every iteration, so if you update a property, it will be saved across iterations. However, many other implementations of IEnumerables return a new set of objects each time, so any changes made will not persist.

Akash KC
  • 16,057
  • 6
  • 39
  • 59