Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Extract row-column from array of objects
Message
From
29/04/2011 03:38:32
 
 
To
28/04/2011 13:24:33
General information
Forum:
ASP.NET
Category:
LINQ
Environment versions
Environment:
C# 4.0
Miscellaneous
Thread ID:
01508607
Message ID:
01508782
Views:
37
>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();
>}
>
Very neat !! And if readability wasn't a factor it could have been just 2 lines for the creation.....
Previous
Reply
Map
View

Click here to load this message in the networking platform