이 예제에서는 LINQ를 사용하여 두 문자열 목록을 비교하고 names1.txt 있지만 names2.txt없는 줄을 출력하는 방법을 보여 줍니다.
데이터 파일을 만들려면
- 방법: 문자열 컬렉션 결합 및 비교(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, DistinctUnion및 Concat)은 메서드 기반 구문으로만 표현할 수 있습니다.
코드 컴파일
Visual Basic 콘솔 애플리케이션 프로젝트를 만들어서, System.Linq 네임스페이스에 대한 Imports
문을 추가합니다.
참고하십시오
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET