Bagikan melalui


Cara: Kueri untuk Kalimat yang Berisi Sekumpulan Kata Yang Ditentukan (LINQ) (Visual Basic)

Contoh ini menunjukkan cara menemukan kalimat dalam file teks yang berisi kecocokan untuk setiap kumpulan kata yang ditentukan. Meskipun array kata kunci pencarian dikodekan secara permanen dalam contoh ini, itu juga dapat diisi secara dinamis pada waktu eksekusi. Dalam contoh ini, kueri mengembalikan kalimat yang berisi kata "Historis," "data," dan "terintegrasi."

Contoh

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

Kueri bekerja dengan terlebih dahulu memisahkan teks menjadi kalimat, lalu memisahkan kalimat menjadi array string yang menyimpan setiap kata. Untuk setiap array ini, metode Distinct menghapus semua kata yang duplikat, kemudian kueri melakukan operasi Intersect pada array kata serta wordsToMatch array. Jika jumlah persimpangan sama dengan jumlah elemen wordsToMatch dalam array, semua kata ditemukan dalam daftar kata dan kalimat aslinya dikembalikan.

Dalam panggilan ke Split, tanda baca digunakan sebagai pemisah untuk menghapusnya dari string. Jika Anda tidak melakukan ini, misalnya Anda dapat memiliki string "Secara historis," yang tidak akan cocok dengan "Secara historis" dalam array wordsToMatch. Anda mungkin harus menggunakan pemisah tambahan, tergantung pada jenis tanda baca yang ditemukan dalam teks sumber.

Mengkompilasi kode

Buat proyek aplikasi konsol Visual Basic, dengan pernyataan Imports untuk namespace System.Linq.

Lihat juga