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

1 comment: