Procedure: Tekenreeksverzamelingen (LINQ) combineren en vergelijken (Visual Basic)
In dit voorbeeld ziet u hoe u bestanden met regels tekst samenvoegt en vervolgens de resultaten sorteert. In het bijzonder ziet u hoe u een eenvoudige samenvoeging, een samenvoeging en een snijpunt kunt uitvoeren op de twee sets tekstregels.
Het project en de tekstbestanden instellen
Kopieer deze namen naar een tekstbestand met de naam names1.txt en sla deze op in de projectmap:
Bankov, Peter Holm, Michael Garcia, Hugo Potra, Cristina Noriega, Fabricio Aw, Kam Foo Beebe, Ann Toyoshima, Tim Guy, Wey Yuan Garcia, Debra
Kopieer deze namen naar een tekstbestand met de naam names2.txt en sla deze op in de projectmap. Houd er rekening mee dat de twee bestanden enkele namen gemeen hebben.
Liu, Jinghao Bankov, Peter Holm, Michael Garcia, Hugo Beebe, Ann Gilchrist, Beth Myrcha, Jacek Giakoumakis, Leo McLin, Nkenge El Yassir, Mehdi
Opmerking
Class ConcatenateStrings
Shared Sub Main()
' Create the IEnumerable data sources.
Dim fileA As String() = System.IO.File.ReadAllLines("../../../names1.txt")
Dim fileB As String() = System.IO.File.ReadAllLines("../../../names2.txt")
' Simple concatenation and sort.
Dim concatQuery = fileA.Concat(fileB).OrderBy(Function(name) name)
' Pass the query variable to another function for execution
OutputQueryResults(concatQuery, "Simple concatenation and sort. Duplicates are preserved:")
' New query. Concatenate files and remove duplicates
Dim uniqueNamesQuery = fileA.Union(fileB).OrderBy(Function(name) name)
OutputQueryResults(uniqueNamesQuery, "Union removes duplicate names:")
' New query. Find the names that occur in both files.
Dim commonNamesQuery = fileA.Intersect(fileB)
OutputQueryResults(commonNamesQuery, "Merge based on intersect: ")
' New query in three steps for better readability
' First filter each list separately
Dim nameToSearch As String = "Garcia"
Dim mergeQueryA As IEnumerable(Of String) = From name In fileA
Let n = name.Split(New Char() {","})
Where n(0) = nameToSearch
Select name
Dim mergeQueryB = From name In fileB
Let n = name.Split(New Char() {","})
Where n(0) = nameToSearch
Select name
' Create a new query to concatenate and sort results. Duplicates are removed in Union.
' Note that none of the queries actually executed until the call to OutputQueryResults.
Dim mergeSortQuery = mergeQueryA.Union(mergeQueryB).OrderBy(Function(str) str)
' Now execute mergeSortQuery
OutputQueryResults(mergeSortQuery, "Concat based on partial name match """ & nameToSearch & """ from each list:")
' Keep console window open in debug mode.
Console.WriteLine("Press any key to exit.")
Console.ReadKey()
End Sub
Shared Sub OutputQueryResults(ByVal query As IEnumerable(Of String), ByVal message As String)
Console.WriteLine(System.Environment.NewLine & message)
For Each item As String In query
Console.WriteLine(item)
Next
Console.WriteLine(query.Count & " total names in list")
End Sub
End Class
' Output:
' Simple concatenation and sort. Duplicates are preserved:
' Aw, Kam Foo
' Bankov, Peter
' Bankov, Peter
' Beebe, Ann
' Beebe, Ann
' El Yassir, Mehdi
' Garcia, Debra
' Garcia, Hugo
' Garcia, Hugo
' Giakoumakis, Leo
' Gilchrist, Beth
' Guy, Wey Yuan
' Holm, Michael
' Holm, Michael
' Liu, Jinghao
' McLin, Nkenge
' Myrcha, Jacek
' Noriega, Fabricio
' Potra, Cristina
' Toyoshima, Tim
' 20 total names in list
' Union removes duplicate names:
' Aw, Kam Foo
' Bankov, Peter
' Beebe, Ann
' El Yassir, Mehdi
' Garcia, Debra
' Garcia, Hugo
' Giakoumakis, Leo
' Gilchrist, Beth
' Guy, Wey Yuan
' Holm, Michael
' Liu, Jinghao
' McLin, Nkenge
' Myrcha, Jacek
' Noriega, Fabricio
' Potra, Cristina
' Toyoshima, Tim
' 16 total names in list
' Merge based on intersect:
' Bankov, Peter
' Holm, Michael
' Garcia, Hugo
' Beebe, Ann
' 4 total names in list
' Concat based on partial name match "Garcia" from each list:
' Garcia, Debra
' Garcia, Hugo
' 2 total names in list
De code compileren
Maak een Visual Basic-consoletoepassingsproject. Voeg een Imports
instructie toe voor de System.Linq-naamruimte.