SGN SGN - 1 year ago 144
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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download