Share via


방법: 여러 소스로 개체 컬렉션 채우기(LINQ)

업데이트: 2008년 7월

이 예제에서는 서로 다른 소스 형식의 데이터를 새 형식의 시퀀스로 병합하는 방법을 보여 줍니다. 다음 코드의 예제는 문자열을 정수 배열과 병합합니다. 그러나 동일한 원칙이 메모리 내 개체(LINQ to SQL 쿼리, ADO.NET 데이터 집합 및 XML 문서의 결과 포함)의 조합을 비롯하여 두 개의 데이터 소스에 적용됩니다.

참고:

메모리 내 데이터나 파일 시스템의 데이터를 아직 데이터베이스에 있는 데이터와 조인하지 마십시오. 데이터베이스 쿼리 및 다른 소스 형식에 대해 조인 작업을 정의할 수 있는 방식이 다양하기 때문에 이러한 도메인 간 조인은 정의되지 않은 결과를 발생시킬 수 있습니다. 또한 이러한 작업은 데이터베이스의 데이터 양이 아주 큰 경우 메모리 부족 예외를 일으킬 수 있는 위험이 있습니다. 데이터베이스의 데이터를 메모리 내 데이터에 조인하려면 먼저 데이터베이스 쿼리에서 ToList 또는 ToArray를 호출한 다음 반환된 컬렉션에서 조인을 수행합니다.

데이터 파일을 만들려면

예제

다음 예제에서는 명명된 형식인 Student를 사용하여 .csv 형식으로 스프레드시트 데이터를 시뮬레이션하는 문자열의 두 메모리 내 컬렉션에서 병합된 데이터를 저장하는 방법을 보여 줍니다. 첫 번째 문자열 컬렉션은 학생 이름과 ID를 나타내며 두 번째 컬렉션은 첫 번째 열의 학생 ID 및 4개의 시험 점수를 나타냅니다.

Class Student
    Public FirstName As String
    Public LastName As String
    Public ID As Integer
    Public ExamScores As List(Of Integer)
End Class

Class PopulateCollections

    Shared Sub Main()

        ' Join content from spreadsheets into a list of Student objectss.
        ' 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, and then projects the results into a new type.

        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.
        ' Note the dynamic creation of a list of ints for the
        ' TestScores member. We skip 1 because the first string
        ' in the array is the student ID, not an exam score.
        Dim scoreQuery1 = From name In names _
                         Let n = name.Split(New Char() {","}) _
                         From id In scores _
                         Let s = id.Split(New Char() {","}) _
                         Where n(2) = s(0) _
                         Select New Student() _
                         With {.FirstName = n(0), .LastName = n(1), .ID = n(2), _
                               .ExamScores = (From scoreAsText In s Skip 1 _
                                             Select Convert.ToInt32(scoreAsText)).ToList()}

        ' Optional. Store the query results for faster access
        ' in future queries. May be useful with very large data files.
        Dim students As List(Of Student) = scoreQuery1.ToList()

        ' Display the list contents
        ' and perform a further calculation
        For Each s In students
            Console.WriteLine("The average score of " & s.FirstName & " " & _
                              s.LastName & " is " & s.ExamScores.Average())
        Next

        ' Keep console window open in debug mode.
        Console.WriteLine("Press any key to exit.")
        Console.ReadKey()
    End Sub
End Class
' Output: 
'The average score of Adams Terry is 85.25
'The average score of Fakhouri Fadi is 92.25
'The average score of Feng Hanying is 88
'The average score of Garcia Cesar is 88.25
'The average score of Garcia Debra is 67
'The average score of Garcia Hugo is 85.75
'The average score of Mortensen Sven is 84.5
'The average score of O'Donnell Claire is 72.25
'The average score of Omelchenko Svetlana is 82.5
'The average score of Tucker Lance is 81.75
'The average score of Tucker Michael is 92
'The average score of Zabokritski Eugene is 83
class Student
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int ID { get; set; }
    public List<int> ExamScores { get; set; }
}

class PopulateCollections
{
    static void Main()
    {
        // These data files are defined in How to: Join Content from Dissimilar Files (LINQ) 
        string[] names = System.IO.File.ReadAllLines(@"../../../names.csv");
        string[] scores = System.IO.File.ReadAllLines(@"../../../scores.csv");

        // Merge the data sources using a named type.
        // var could be used instead of an explicit type.
        // Note the dynamic creation of a list of ints for the
        // TestScores member. We skip 1 because the first string
        // in the array is the student ID, not an exam score.
        IEnumerable<Student> queryNamesScores =
            from name in names
            let x = name.Split(',')
            from score in scores
            let s = score.Split(',')
            where x[2] == s[0]
            select new Student()
            {
                FirstName = x[0],
                LastName = x[1],
                ID = Convert.ToInt32(x[2]),
                ExamScores = (from scoreAsText in s.Skip(1)
                              select Convert.ToInt32(scoreAsText)).
                              ToList()
            };

        // Optional. Store the newly created student objects in memory
        // for faster access in future queries. Could be useful with
        // very large data files.
        List<Student> students = queryNamesScores.ToList();

        // Display the results and perform one further calculation.
        foreach (var student in students)
        {
            Console.WriteLine("The average score of {0} {1} is {2}.",
                student.FirstName, student.LastName, student.ExamScores.Average());
        }

        //Keep console window open in debug mode
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
/* Output: 
    The average score of Adams Terry is 85.25.
    The average score of Fakhouri Fadi is 92.25.
    The average score of Feng Hanying is 88.
    The average score of Garcia Cesar is 88.25.
    The average score of Garcia Debra is 67.
    The average score of Garcia Hugo is 85.75.
    The average score of Mortensen Sven is 84.5.
    The average score of O'Donnell Claire is 72.25.
    The average score of Omelchenko Svetlana is 82.5.
    The average score of Tucker Lance is 81.75.
    The average score of Tucker Michael is 92.
    The average score of Zabokritski Eugene is 83.
 */

이러한 예제의 데이터 소스는 개체 이니셜라이저로 초기화됩니다. 쿼리에서는 join 절을 사용하여 이름을 점수와 일치시킵니다. ID는 외래 키로 사용됩니다. 그러나 한 소스에서 ID는 문자열이며 다른 소스에서는 정수입니다. join에서 같음 비교가 필요하므로 먼저 문자열에서 ID를 추출하고 이를 정수로 변환해야 합니다. 이 작업은 두 개의 let 절에서 수행됩니다. 첫 번째 let 절의 임시 식별자 x는 각 공간에서 원래 문자열을 분할하여 생성된 세 문자열의 배열을 저장합니다. 두 번째 let 절의 식별자 n은 ID 하위 문자열을 정수로 변환한 결과를 저장합니다. select 절에서 개체 이니셜라이저는 두 소스의 데이터를 사용하여 새 Student 개체를 각각 인스턴스화하는 데 사용됩니다.

쿼리 결과를 저장할 필요가 없는 경우 익명 형식이 명명된 형식보다 더 편리할 수 있습니다. 쿼리가 실행되는 메서드 외부에 쿼리 결과를 전달하는 경우에는 명명된 형식이 필요합니다. 다음 예제에서는 명명된 형식 대신 익명 형식을 사용하여 위의 예제와 동일한 작업을 수행합니다.

' This query uses an anonymous type
' Note the dynamic creation of a list of ints for the
' TestScores member. We skip 1 because the first string
' in the array is the student ID, not an exam score.
Dim scoreQuery2 = From name In names _
                 Let n = name.Split(New Char() {","}) _
                 From id In scores _
                 Let s = id.Split(New Char() {","}) _
                 Where n(2) = s(0) _
                 Select New With {.Last = n(0), _
                                  .First = n(1), _
                                  .TestScores = (From scoreAsText In s Skip 1 _
                                     Select Convert.ToInt32(scoreAsText)).ToList()}

' Display the list contents
' and perform a further calculation
For Each s In scoreQuery2
    Console.WriteLine("The average score of " & s.First & " " & s.Last & " is " & s.TestScores.Average())
Next
// Merge the data sources by using an anonymous type.
// Note the dynamic creation of a list of ints for the
// TestScores member. We skip 1 because the first string
// in the array is the student ID, not an exam score.
var queryNamesScores2 =
    from name in names
    let x = name.Split(',')
    from score in scores
    let s = score.Split(',')
    where x[2] == s[0]
    select new 
    {
        First = x[0],
        Last = x[1],
        TestScores = (from scoreAsText in s.Skip(1)
                      select Convert.ToInt32(scoreAsText))
                      .ToList()
    };

// Display the results and perform one further calculation.
foreach (var student in queryNamesScores2)
{
    Console.WriteLine("The average score of {0} {1} is {2}.",
        student.First, student.Last, student.TestScores.Average());
}

코드 컴파일

  • .NET Framework 버전 3.5를 대상으로 하는 Visual Studio 프로젝트를 만듭니다. 기본적으로 프로젝트에는 System.Core.dll에 대한 참조 및 System.Linq 네임스페이스에 대한 using 지시문(C#) 또는 Imports 문(Visual Basic)이 있습니다.

  • 프로젝트에 이 코드를 복사합니다.

  • F5 키를 눌러 프로그램을 컴파일하고 실행합니다.

  • 아무 키나 눌러 콘솔 창을 닫습니다.

참고 항목

개념

LINQ 및 문자열

참조

개체 및 컬렉션 이니셜라이저(C# 프로그래밍 가이드)

익명 형식(C# 프로그래밍 가이드)

변경 기록

날짜

변경 내용

원인

2008년 7월

두 번째 코드 예제 집합을 추가했습니다.

콘텐츠 버그 수정