方法 : 異種ファイルのコンテンツを結合する (LINQ)

この例では、一致するキーとして 1 つの共通の値を持つ 2 つのコンマ区切りファイルのデータを結合する方法を示します。 この方法は、2 つのスプレッドシートのデータを結合する場合や、スプレッドシートと異なる形式のファイルのデータとを新しいファイルに結合する場合に役立ちます。 この例を変更することで、あらゆる構造化テキスト ファイルに応用できます。

データ ファイルを作成するには

  1. 以下の行を scores.csv という名前のファイルにコピーし、プロジェクト ファイルと同じフォルダーに保存します。 たとえば、プロジェクト ファイルが Visual Studio 2010\Projects にある場合は、scores.csv をそのフォルダーに保存します。 このファイルは、スプレッドシートのデータを表します。 列 1 は学生の ID、列 2 ~ 5 はテストの得点です。

    111, 97, 92, 81, 60
    112, 75, 84, 91, 39
    113, 88, 94, 65, 91
    114, 97, 89, 85, 82
    115, 35, 72, 91, 70
    116, 99, 86, 90, 94
    117, 93, 92, 80, 87
    118, 92, 90, 83, 78
    119, 68, 79, 88, 92
    120, 99, 82, 81, 79
    121, 96, 85, 91, 60
    122, 94, 92, 91, 91
    
  2. 以下の行を names.csv という名前のファイルにコピーし、プロジェクト ファイルと同じフォルダーに保存します。 たとえば、プロジェクト ファイルが Visual Studio 2010\Projects にある場合は、names.csv をそのフォルダーに保存します。 このファイルは、学生の姓、名、および ID が含まれるスプレッドシートを表します。

    Omelchenko,Svetlana,111
    O'Donnell,Claire,112
    Mortensen,Sven,113
    Garcia,Cesar,114
    Garcia,Debra,115
    Fakhouri,Fadi,116
    Feng,Hanying,117
    Garcia,Hugo,118
    Tucker,Lance,119
    Adams,Terry,120
    Zabokritski,Eugene,121
    Tucker,Michael,122
    

使用例

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() {","}) 
                            From id In scores 
                            Let n2 = id.Split(New Char() {","}) 
                            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
class JoinStrings
{
    static void Main()
    {
        // Join content from dissimilar files that contain
        // related information. File names.csv contains the student
        // name plus an ID number. File 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.

        string[] names = System.IO.File.ReadAllLines(@"../../../names.csv");
        string[] scores = System.IO.File.ReadAllLines(@"../../../scores.csv");


        // Name:    Last[0],       First[1],  ID[2]
        //          Omelchenko,    Svetlana,  11
        // 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.
        IEnumerable<string> scoreQuery1 =
            from name in names
            let nameFields = name.Split(',')
            from id in scores
            let scoreFields = id.Split(',')
            where nameFields[2] == scoreFields[0]
            select nameFields[0] + "," + scoreFields[1] + "," + scoreFields[2] 
                   + "," + scoreFields[3] + "," + scoreFields[4];

        // Pass a query variable to a method and execute it
        // in the method. 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();
    }

    static void OutputQueryResults(IEnumerable<string> query, string message)
    {
        Console.WriteLine(System.Environment.NewLine + message);
        foreach (string item in query)
        {
            Console.WriteLine(item);
        }
        Console.WriteLine("{0} total names in list", query.Count());
    }
}
/* Output:
Merge two spreadsheets:
Adams, 99, 82, 81, 79
Fakhouri, 99, 86, 90, 94
Feng, 93, 92, 80, 87
Garcia, 97, 89, 85, 82
Garcia, 35, 72, 91, 70
Garcia, 92, 90, 83, 78
Mortensen, 88, 94, 65, 91
O'Donnell, 75, 84, 91, 39
Omelchenko, 97, 92, 81, 60
Tucker, 68, 79, 88, 92
Tucker, 94, 92, 91, 91
Zabokritski, 96, 85, 91, 60
12 total names in list
 */

コードのコンパイル

  • .NET Framework Version 3.5 以降のバージョンを対象とする Visual Studio プロジェクトを作成します。 既定のプロジェクトには、System.Core.dll への参照と、System.Linq 名前空間に対する using ディレクティブ (C#) または Imports ステートメント (Visual Basic) が含まれます。 C# プロジェクトでは、System.IO 名前空間に対する using ディレクティブを追加します。

  • このコードをプロジェクト内にコピーします。

  • F5 キーを押して、プログラムをコンパイルおよび実行します。

  • 任意のキーを押して、コンソール ウィンドウを終了します。

参照

概念

LINQ と文字列

LINQ とファイル ディレクトリ