Frage

How Can I set to lower all the elements of an string array using LINQ?

Dim fileExtensions() As String = {"Mp3", "mP4", "wMw", "weBM", Nothing, ""}

Dim ToLower_fileExtensions = fileExtensions().Select...

(not using For)

War es hilfreich?

Lösung

Try this:

Dim ToLower_fileExtensions = From w in fileExtensions Select IF(w Is Nothing, Nothing, w.ToLower())

Andere Tipps

The easy and efficient way:

For i As Int32 = 0 To fileExtensions.Length - 1
    fileExtensions(i) = fileExtensions(i).ToLower()
Next

Since you've asked for linq, this is less efficient since it needs to create a new collection:

fileExtensions = fileExtensions.Select(Function(str) str.ToLower()).ToArray()

Just a simple and generic function I've did based on @dasblinkenlight solution:

Private Function ArrayToLower(ByVal [Array] As IEnumerable) As IEnumerable

    Return From str In [Array] _
            Select If(String.IsNullOrEmpty(str), _
                      String.Empty, _
                      str.ToLower())

End Function

PS: Nice to convert it to a extension

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top