Condividi tramite


Procedura: popolare insiemi di oggetti da più origini (LINQ)

Aggiornamento: Luglio 2008

In questo esempio viene illustrato come unire dati da tipi di origine diversi in una sequenza di nuovi tipi. Negli esempi del codice seguente vengono unite stringhe con matrici di valori interi. Lo stesso principio può però essere applicato a due origini dati qualsiasi, inclusa una combinazione di oggetti in memoria, ad esempio i risultati di query LINQ to SQL, dataset ADO.NET e documenti XML.

Nota:

Non tentare di unire dati in memoria o dati nel file system con dati che sono ancora presenti in un database. Tali join tra i domini possono generare risultati non definiti a causa dei diversi modi in cui le operazioni di join possono essere definite per le query di database e gli altri tipi di origini. Inoltre, è possibile che tale operazione generi un'eccezione di memoria insufficiente se la quantità di dati nel database è elevata. Per unire i dati da un database nei dati in memoria, chiamare prima ToList o ToArray sulla query di database, quindi eseguire il join sull'insieme restituito.

Per creare il file di dati

Esempio

Nell'esempio seguente viene illustrato come utilizzare un tipo Student denominato per archiviare i dati uniti da due insiemi di stringhe in memoria che simulano i dati di un foglio di calcolo in formato CSV. Il primo insieme di stringhe rappresenta i nomi e gli ID degli studenti e il secondo insieme rappresenta l'ID studente (nella prima colonna) e i punteggi di quattro esami.

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.
 */

Le origini dati in questi esempi vengono inizializzate con gli inizializzatori di oggetto. La query utilizza una clausola join per creare una corrispondenza tra i nomi e i punteggi. ID viene utilizzato come chiave esterna. Tuttavia, in un'origine l'ID è rappresentato da una stringa mentre nell'altra origine è rappresentato da un numero intero. Poiché la clausola join richiede un confronto di uguaglianza, è necessario prima estrarre l'ID dalla stringa e convertirlo in un numero intero mediante due clausole let. L'identificatore temporaneo x nella prima clausola let archivia una matrice di tre stringhe create suddividendo la stringa originale a ogni spazio. L'identificatore n nella seconda clausola let archivia il risultato della conversione della sottostringa ID in un numero intero. Nella clausola select viene utilizzato un inizializzatore di oggetto per creare un'istanza di ogni nuovo oggetto Student utilizzando i dati dalle due origini.

Se non è necessario archiviare i risultati di una query, è più conveniente utilizzare i tipi anonimi anziché i tipi denominati. I tipi denominati sono necessari se i risultati della query vengono passati all'esterno del metodo in cui è stata eseguita la query. Nell'esempio seguente viene eseguita la stessa attività dell'esempio precedente, ma vengono utilizzati tipi anonimi anziché tipi denominati:

' 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());
}

Compilazione del codice

  • Creare un progetto di Visual Studio destinato a .NET Framework versione 3.5. Per impostazione predefinita, il progetto contiene un riferimento a System.Core.dll e una direttiva using (C#) o un'istruzione Imports (Visual Basic) per lo spazio dei nomi System.Linq.

  • Copiare questo codice nel progetto.

  • Premere F5 per compilare ed eseguire il programma.

  • Premere un tasto per chiudere la finestra della console.

Vedere anche

Concetti

LINQ e stringhe

Riferimenti

Inizializzatori di oggetto e di insieme (Guida per programmatori C#)

Tipi anonimi (Guida per programmatori C#)

Cronologia delle modifiche

Date

History

Motivo

Luglio 2008

Aggiunto secondo gruppo di esempi di codice.

Correzione di errori nel contenuto.