How to: Find the Set Difference Between Two Lists (LINQ) (Visual Basic)
This example shows how to use LINQ to compare two lists of strings and output those lines that are in names1.txt but not in names2.txt.
To create the data files
- Copy names1.txt and names2.txt to your solution folder as shown in How to: Combine and Compare String Collections (LINQ) (Visual Basic).
Example
Class CompareLists
Shared Sub Main()
' Create the IEnumerable data sources.
Dim names1 As String() = System.IO.File.ReadAllLines("../../../names1.txt")
Dim names2 As String() = System.IO.File.ReadAllLines("../../../names2.txt")
' Create the query. Note that method syntax must be used here.
Dim differenceQuery = names1.Except(names2)
Console.WriteLine("The following lines are in names1.txt but not names2.txt")
' Execute the query.
For Each name As String In differenceQuery
Console.WriteLine(name)
Next
' Keep console window open in debug mode.
Console.WriteLine("Press any key to exit.")
Console.ReadKey()
End Sub
End Class
' Output:
' The following lines are in names1.txt but not names2.txt
' Potra, Cristina
' Noriega, Fabricio
' Aw, Kam Foo
' Toyoshima, Tim
' Guy, Wey Yuan
' Garcia, Debra
Some types of query operations in Visual Basic, such as Except, Distinct, Union, and Concat, can only be expressed in method-based syntax.
Compile the code
Create a Visual Basic console application project, with an Imports
statement for the System.Linq namespace.
See also
Sodelujte z nami v storitvi GitHub
Vir za to vsebino najdete v storitvi GitHub, kjer lahko tudi ustvarite in pregledate težave in zahtevke za uveljavitev sprememb. Če želite več informacij, glejte naš vodnik za sodelavce.