SGN SGN - 9 months ago 57
C# Question

Adding string to IEnumerable of type string using LINQ to entities

I have a collection of objects, that have a property of type


My code

foreach (var student in students)
string term = SomeMethodhere();

students.Where(x => x.Id == student.Id)
.ForEach(x =>

I have no erros, but
, remain unchanged.

What am I missing?

Answer Source

x.FieldsOfStudy.ToList() produces a new instance of list. So you are actually adding element to new collection.

Possible workarounds (if you can set new instace of list to property FieldsOfStudy):

   var list = x.FieldsOfStudy as IList<string> ?? new List<string>(x.FieldsOfStudy);
   x.FieldsOfStudy = list;