How to Convert all strings in List<string> to lower case using LINQ?

Max Schilling picture Max Schilling · Oct 23, 2008 · Viewed 94.6k times · Source

I saw a code snippet yesterday in one of the responses here on StackOverflow that intrigued me. It was something like this:

 List<string> myList = new List<string> {"aBc", "HELLO", "GoodBye"};

 myList.ForEach(d=>d.ToLower());

I was hoping I could use it to convert all items in myList to lowercase. However, it doesn't happen... after running this, the casing in myList is unchanged.

So my question is whether there IS a way, using LINQ and Lambda expressions to easily iterate through and modify the contents of a list in a manner similar to this.

Thanks, Max

Answer

Jason Bunting picture Jason Bunting · Oct 23, 2008

Easiest approach:

myList = myList.ConvertAll(d => d.ToLower());

Not too much different than your example code. ForEach loops the original list whereas ConvertAll creates a new one which you need to reassign.