I have the following loop:
for (var i = 0; i < myStringList.Count; i++)
{
myStringList[i] = myStringList[i].ToUpper();
}
into a Linq expression?
Use this
myStringList = myStringList.Select(x => x.ToUpper()).ToList();
I have used .ToList() in the end assuming that myStringList is a List<string>.
FoodforThought: In List there is a method ForEach() which performs an action on each item like this.
myStringList.ForEach(x => x.Foo = Bar);
But that cannot be used here as that method can be used to change a property of an item but cannot be used to change the item itself.
So this will not do anything
myStringList.ForEach(x => x = x.ToUpper());
myStringList = myStringList.Select(x => x.ToUpper()).ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With