Easiest way to Convert List to Lower case using LINQ

A user recently asked me a question. He had a List<string> collection that he obtained from a service. He wanted to convert the strings in the List<string> into lower case and then bind it to an ASP.NET DropDownList. He asked me for the simplest way to do so. Here's the solution I suggested him using LINQ:

C#


// Convert to Lower case and bind to dropdownlist

List<string> strList = new List<string> 

{ "One", "TWO", "THree", "Four", "five" };        

strList = strList.ConvertAll(low => low.ToLowerInvariant());

foreach (string s in strList)

{

    DropDownList1.Items.Add(s);

}



VB.NET


        Dim strList As List(Of String) = _

        New List(Of String)(New String() _

                    {"One", "TWO", "THree", "Four", "five"})

        strList = strList.ConvertAll(Function(low)_
        low.ToLowerInvariant())

        For Each s As String In strList

            DropDownList1.Items.Add(s)

        Next s


Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.