Class JoinStrings
Shared Sub Main()
' Join content from spreadsheet files that contain
' related information. names.csv contains the student name
' plus an ID number. scores.csv contains the ID and a
' set of four test scores. The following query joins
' the scores to the student names by using ID as a
' matching key.
Dim names As String() = System.IO.File.ReadAllLines("../../../names.csv")
Dim scores As String() = System.IO.File.ReadAllLines("../../../scores.csv")
' Name: Last[0], First[1], ID[2], Grade Level[3]
' Omelchenko, Svetlana, 111, 2
' Score: StudentID[0], Exam1[1] Exam2[2], Exam3[3], Exam4[4]
' 111, 97, 92, 81, 60
' This query joins two dissimilar spreadsheets based on common ID value.
' Multiple from clauses are used instead of a join clause
' in order to store results of id.Split.
Dim scoreQuery1 = From name In names _
Let n = name.Split(New Char() {","c}) _
From id In scores _
Let n2 = id.Split(New Char() {","c}) _
Where n(2) = n2(0) _
Select n(0) & "," & n(1) & "," & n2(0) & "," & n2(1) & "," _
& n2(2) & "," & n2(3)
' Pass a query variable to a Sub and execute it there.
' The query itself is unchanged.
OutputQueryResults(scoreQuery1, "Merge two spreadsheets:")
' 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:
'Merge two spreadsheets:
'Adams,Terry,120, 99, 82, 81
'Fakhouri,Fadi,116, 99, 86, 90
'Feng,Hanying,117, 93, 92, 80
'Garcia,Cesar,114, 97, 89, 85
'Garcia,Debra,115, 35, 72, 91
'Garcia,Hugo,118, 92, 90, 83
'Mortensen,Sven,113, 88, 94, 65
'O'Donnell,Claire,112, 75, 84, 91
'Omelchenko,Svetlana,111, 97, 92, 81
'Tucker,Lance,119, 68, 79, 88
'Tucker,Michael,122, 94, 92, 91
'Zabokritski,Eugene,121, 96, 85, 91
'12 total names in list