다음을 통해 공유


방법: 두 목록 간의 차이점 설정 찾기(LINQ)(Visual Basic)

이 예제에서는 LINQ를 사용하여 두 문자열 목록을 비교하고 names1.txt 있지만 names2.txt없는 줄을 출력하는 방법을 보여 줍니다.

데이터 파일을 만들려면

  1. 방법: 문자열 컬렉션 결합 및 비교(LINQ) (Visual Basic)에 표시된 대로 names1.txt과 names2.txt을 솔루션 폴더에 복사합니다.

예시

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  

Visual Basic의 일부 쿼리 작업 유형(예: Except, DistinctUnionConcat)은 메서드 기반 구문으로만 표현할 수 있습니다.

코드 컴파일

Visual Basic 콘솔 애플리케이션 프로젝트를 만들어서, System.Linq 네임스페이스에 대한 Imports 문을 추가합니다.

참고하십시오