schlebe
schlebe

Reputation: 3716

How to filter String Array in VB.Net?

What is VB.Net code to filter a String Array ?

I use following code

Imports System.Reflection
Dim ass As Assembly = Assembly.GetExecutingAssembly()
Dim resourceName() As String = ass.GetManifestResourceNames()

that return a String array

How can I filter resourceName() variable ?

I tried following lines of code

    Dim sNameList() As String 
        = resourceName.FindAll(Function(x As String) x.EndsWith("JavaScript.js"))

but compiler return following error

BC36625: Lambda expression cannot be converted to 'T()' because 'T()' is not a delegate type

How can I correct this error ? Is there another solution to solve my problem ?

Upvotes: 1

Views: 1633

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54417

Dim sNameList = resourceName.Where(Function(s) s.EndsWith("JavaScript.js"))

In that case, sNameList is an IEnumerable(Of String), which is all you need if you intend to use a For Each loop over it. If you genuinely need an array:

Dim sNameList = resourceName.Where(Function(s) s.EndsWith("JavaScript.js")).ToArray()

The reason that your existing code didn't work is that Array.FindAll is Shared and so you call it on the Array class, not an array instance:

Dim sNameList = Array.FindAll(resourceName, Function(s) s.EndsWith("JavaScript.js"))

Upvotes: 3

Related Questions