Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Extract row-column from array of objects
Message
De
28/04/2011 13:24:33
 
 
À
28/04/2011 09:10:19
Information générale
Forum:
ASP.NET
Catégorie:
LINQ
Versions des environnements
Environment:
C# 4.0
Divers
Thread ID:
01508607
Message ID:
01508685
Vues:
35
If you are looking to create a DataTable, the following might work:
DataTable newTable = new DataTable();

if (result.Any())
{
    // Assume all rows have the same structure, use the first to create the columns
    var rowForColStructure = result.OfType<GLWebService.goldlasso.anyTypeArray1>().First();

    // Create the structures with a generic name
    DataColumn[] columns = rowForColStructure.item.Select((item, index) => new DataColumn(string.Format("Column{0}", index), item.GetType())).ToArray();
    // Add the columns to the table                
    newTable.Columns.AddRange(columns);
    
    // New rows get added when the query is executed via the ToList
    var rows = result.OfType<GLWebService.goldlasso.anyTypeArray1>().Select(item => newTable.Rows.Add(item.item)).ToList();
}
The items would still be in their original types. If you need it as strings, change it to:
DataTable newTable = new DataTable();

if (result.Any())
{
    // Assume all rows have the same structure, use the first to create the columns
    var rowForColStructure = result.OfType<GLWebService.goldlasso.anyTypeArray1>().First();

    // Create the structures with a generic name
    DataColumn[] columns = rowForColStructure.item.Select((item, index) => new DataColumn(string.Format("Column{0}", index), typeof(string))).ToArray();
    // Add the columns to the table                
    newTable.Columns.AddRange(columns);
    
    // New rows get added when the query is executed via the ToList
    var rows = result.OfType<GLWebService.goldlasso.anyTypeArray1>().Select(item => newTable.Rows.Add(item.item)).ToList();
}
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform