Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 为什么列表(字符串).indexof(函数)不起作用?_Vb.net_Lambda - Fatal编程技术网

Vb.net 为什么列表(字符串).indexof(函数)不起作用?

Vb.net 为什么列表(字符串).indexof(函数)不起作用?,vb.net,lambda,Vb.net,Lambda,我非常喜欢Lambdas,但我对这个表达方式有困难: Dim Test as New List/of String) Test.add("A") Test.add("B") Dim Adress=Test.Indexof(Function (adr) (Trim(Ucase(Adr)) LIke "A") 编译器警告Stirng不是委托,也不会编译-有什么办法解决这个问题吗?该方法将字符串作为输入 函数function(adr)Trim(UCase(adr))类似于“A”是一个委托,而不是字

我非常喜欢Lambdas,但我对这个表达方式有困难:

Dim Test as New List/of String)
Test.add("A")
Test.add("B")

Dim Adress=Test.Indexof(Function (adr) (Trim(Ucase(Adr)) LIke "A")
编译器警告Stirng不是委托,也不会编译-有什么办法解决这个问题吗?

该方法将字符串作为输入

函数
function(adr)Trim(UCase(adr))类似于“A”
是一个委托,而不是字符串,因此不能将其与IndexOf方法一起使用

如果您想执行搜索忽略案例,那么下面的LINQ查询可能会对您很好

Test.First(Function (adr) adr.Equals("A", StringComparison.OrdinalIgnoreCase))

List.IndexOf
获取
T
并返回该对象的索引。所以不能在这里传递谓词。我假设您希望地址的第一个索引等于忽略大小写“a”。然后您可以使用Linq:

Dim matches = Test.Select(Function(Address, Index) New With {Address, Index}).
                   Where(Function(x) x.Address.ToUpper = "A")
If matches.Any() Then
    Dim firstMatch = matches.First()
    Dim firstMatchIndex As Int32 = firstMatch.Index '  0 
    Dim firstMatchAddress As String = firstMatch.Address ' "A"
End If

工作起来很有魅力,谢谢-只是为了理解一个问题:为什么地址被初始化为字符串,而索引被初始化为整数?@ChristianSauer:因为您的泛型列表包含字符串,第一个参数的类型是字符串,第二个参数的类型(如果您使用或
可枚举的重载。其中
)始终是序列中项目的索引。请注意,您只能使用LINQ的方法语法获取索引,而不能在查询中获取索引。