2

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)

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417

3 Answers3

2

Try this:

Dim ToLower_fileExtensions = From w in fileExtensions Select IF(w Is Nothing, Nothing, w.ToLower())
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • It works fine for normal strings but throws an exception for empty fields (Nothing, ""), can you please update it with the needed clausures ? I think I need to add a where clausule to check if the field is empty or not. – ElektroStudios Oct 17 '13 at 09:29
  • Done, thanks again: From w In fileExtensions Where Not String.IsNullOrEmpty(w) Select w.ToLower() – ElektroStudios Oct 17 '13 at 09:30
  • 1
    @ElektroHacker That would eliminate `Nothing`s; my edit would preserve them. – Sergey Kalinichenko Oct 17 '13 at 09:32
2

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()
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
-1

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

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417