共用方式為


如何:查詢包含指定單字集的句子 (LINQ) (Visual Basic)

此範例示範如何在文件中尋找包含指定詞語集合中匹配內容的句子。 儘管此範例中的搜尋字詞陣列是硬性編碼的,但它也可以在運行時動態填入。 在此範例中,查詢會傳回包含 「Historically」、“data” 和 “integrated” 字組的句子。

範例

Class FindSentences

    Shared Sub Main()
        Dim text As String = "Historically, the world of data and the world of objects " &
        "have not been well integrated. Programmers work in C# or Visual Basic " &
        "and also in SQL or XQuery. On the one side are concepts such as classes, " &
        "objects, fields, inheritance, and .NET Framework APIs. On the other side " &
        "are tables, columns, rows, nodes, and separate languages for dealing with " &
        "them. Data types often require translation between the two worlds; there are " &
        "different standard functions. Because the object world has no notion of query, a " &
        "query can only be represented as a string without compile-time type checking or " &
        "IntelliSense support in the IDE. Transferring data from SQL tables or XML trees to " &
        "objects in memory is often tedious and error-prone."

        ' Split the text block into an array of sentences.
        Dim sentences As String() = text.Split(New Char() {".", "?", "!"})

        ' Define the search terms. This list could also be dynamically populated at run time
        Dim wordsToMatch As String() = {"Historically", "data", "integrated"}

        ' Find sentences that contain all the terms in the wordsToMatch array
        ' Note that the number of terms to match is not specified at compile time
        Dim sentenceQuery = From sentence In sentences
                            Let w = sentence.Split(New Char() {" ", ",", ".", ";", ":"},
                                                   StringSplitOptions.RemoveEmptyEntries)
                            Where w.Distinct().Intersect(wordsToMatch).Count = wordsToMatch.Count()
                            Select sentence

        ' Execute the query
        For Each str As String In sentenceQuery
            Console.WriteLine(str)
        Next

        ' Keep console window open in debug mode.
        Console.WriteLine("Press any key to exit.")
        Console.ReadKey()
    End Sub

End Class
' Output:
' Historically, the world of data and the world of objects have not been well integrated

查詢的運作方式是先將文字分割成句子,然後將句子分割成保存每個單字的字串數位。 對於這些陣列Distinct,方法會移除所有重複的字組,然後查詢會在文字陣列和Intersect陣列上執行wordsToMatch作業。 如果交集的計數與陣列的計數 wordsToMatch 相同,則會在單字中找到所有單字,並傳回原始句子。

在對Split的呼叫中,標點符號會被用作分隔符,以便將它們從字串中移除。 如果您未這麼做,例如,您可能有字串 「Historically」,該字串與陣列中的 wordsToMatch “Historically” 不相符。 您可能需要使用其他分隔符,視來源文字中找到的標點符號類型而定。

編譯程式碼

建立一個包含 Imports 語句以使用 System.Linq 命名空間的 Visual Basic 控制台應用程式專案。

另請參閱