Is it possible to write a lambda expression that will iterate through array of objects and replace all occurences of ‘X’, ‘Y’, ‘ ‘ and ‘Z’ in one of the properties?
E.g.
return query.Select(x => { x.SomePropertyName= x.SomePropertyName.Trim().Replace(' ', "_"); return x; }).ToList();
For some reason, a query above doesn’t replace a single character, when I need to replace multiple characters.
Thank you
When I want to replace one of a number of characters with one single other character, I often use a combination of string.Split and string.Join:
This will replace any occurrence of
'X','Y'or'Z'with'_'. You can combine this with the looping construct of your choice.As discussed in the comments, using
Selectdoes not really add any value in this case. A normalforeachloop would do the job, and would even produce more compact code: