Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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
Arrays 如果字符串包含VB.NET中LINQ字符串数组中的任何项,如何返回True_Arrays_String_Vb.net_Function_Linq - Fatal编程技术网

Arrays 如果字符串包含VB.NET中LINQ字符串数组中的任何项,如何返回True

Arrays 如果字符串包含VB.NET中LINQ字符串数组中的任何项,如何返回True,arrays,string,vb.net,function,linq,Arrays,String,Vb.net,Function,Linq,我在堆栈溢出上找不到这个问题,但如果它在这里,请让我知道,我会记下来 在VB.NET中使用LINQ,如果字符串包含字符串数组中的一个项,如何返回True 这是多行代码。如何使用VB.NET中的LINQ在一行中实现这一点 Sub Main Dim endPointTimeoutText As Array = {"endpoint timeout", "endpoint is not available"} Dim strResult As

我在堆栈溢出上找不到这个问题,但如果它在这里,请让我知道,我会记下来

在VB.NET中使用LINQ,如果字符串包含字符串数组中的一个项,如何返回True

这是多行代码。如何使用VB.NET中的LINQ在一行中实现这一点

Sub Main

    Dim endPointTimeoutText As Array = {"endpoint timeout", "endpoint is not available"}

    Dim strResult As String = "endpoint is not available sample text."
    
    Dim booleanResult As Boolean = False

    For Each item As String In endPointTimeoutText

        If strResult.Contains(item) Then

            booleanResult = True
            Exit For

        End If

    Next
    
    Console.WriteLine(booleanResult) 'Only included this for the example
    
End Sub

根据字符串(strResult)是否包含字符串数组(endPointTimeoutText)中的一个值,预期结果将为“True”或“False”

如果您在心理上改变它,不要问“对于此字符串X,此数组中的哪些内容在该字符串中”,您会问“对于此字符串数组,其中哪些字符串位于此字符串X中:”

顺便说一下,如果你这样做的话,它会让你的代码读得更好,这样事物的集合就有了多个名字。
Dim wasATimeout = endPointTimeoutTexts.Any(Function(ett) strResult.Contains(ett))

这很微妙,但在可读性方面意义重大

谢谢Caius Jard在这方面的帮助。我将发布完整的程序,作为下面的答案

我需要使用列表而不是数组,这样才能使用'Any()'方法。再次感谢Caius,我非常感谢

Sub Main
    
    Dim endPointTimeoutText As String = "endpoint timeout,endpoint is not available"
    
    Dim endPointTimeoutList As New List(Of String)

    Dim strResult As String = "endpoint is not available sample text."
    
    endPointTimeoutList = endPointTimeoutText.Split(",").ToList()

    Dim areAnyStringsPresent As Boolean
    
    areAnyStringsPresent = endPointTimeoutList.Any(Function(itemInEndPointTimeoutList) strResult.Contains(itemInEndPointTimeoutList))
    
    Console.WriteLine(areAnyStringsPresent)
    
    'This code produces the following output:
    
        'True
    
End Sub

您本来不应该使用
数组
类型。这就是问题所在。该类型应该是
String()
Dim endPointTimeoutText As Array={“端点超时”,“端点不可用”}
应该是
Dim endPointTimeoutText As String()={“端点超时”,“端点不可用”}
类型端点不可用“}或者甚至只是
Dim endPointTimeoutText={“endpoint timeout”,“endpoint不可用”}
并且可以推断出正确的类型。
Any
可以在任何
IEnumerable(Of T)
上调用,它是
字符串()
只是一个
数组
不是。谢谢。这是一个很好的提示。对于这个特殊的问题,我必须从一个字符串开始,并且我使用了一个字符串数组,尽管我在示例中对其进行了不同的编码。你的理解很好。我感谢你的建议。谢谢,马克。
Sub Main
    
    Dim endPointTimeoutText As String = "endpoint timeout,endpoint is not available"
    
    Dim endPointTimeoutList As New List(Of String)

    Dim strResult As String = "endpoint is not available sample text."
    
    endPointTimeoutList = endPointTimeoutText.Split(",").ToList()

    Dim areAnyStringsPresent As Boolean
    
    areAnyStringsPresent = endPointTimeoutList.Any(Function(itemInEndPointTimeoutList) strResult.Contains(itemInEndPointTimeoutList))
    
    Console.WriteLine(areAnyStringsPresent)
    
    'This code produces the following output:
    
        'True
    
End Sub