共用方式為


如何:查詢包含指定字組的句子 (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 方法會移除所有重複的字組,接著查詢會對字組陣列和 wordsToMatch 陣列執行 Intersect 作業。 如果交集的計數與 wordsToMatch 陣列的計數相同,則所有字都可以在字組中找到,因而傳回原始句子。

Split 呼叫中,標點符號會當成分隔符號,以便從字串中移除。 如果您不這麼做,則您的字串 "Historically," 與 wordsToMatch 陣列中的 "Historically" 不符。 根據來源文字中找到的標點符號類型,您可能必須使用其他分隔符號。

編譯程式碼

使用 System.Linq 命名空間的 Imports 陳述式建立 Visual Basic 主控台應用程式專案。

另請參閱