2016-06-08 69 views
5

Ho un elenco nidificato e ho bisogno di convertirlo in DataSet in C#. Ho trovato molti esempi su questo, ma non fanno quello che mi serve. Ho una lista in un elenco, ho bisogno dell'elenco annidato in un altro DataTable nel DataSet.Come convertire l'elenco nidificato in Dataset in C#

Ecco un esempio di lista

public class InvoiceResponse(){ 
    public string BillToName { get; set; } 
    public string BillToAddr1 { get; set; } 
    ... 
    ... 
    public List<InvoiceItemResponse> Items { get; set; }   
} 

Ho usato il codice qui sotto per convertire l'elenco di DataSet, ma non ha ancora convertire gli elementi di DataTable nel set di dati

public DataSet CreateDataSet<T>(List<T> list) 
{ 
    //list is nothing or has nothing, return nothing (or add exception handling) 
    if (list == null || list.Count == 0) { return null; } 

    //get the type of the first obj in the list 
    var obj = list[0].GetType(); 

    //now grab all properties 
    var properties = obj.GetProperties(); 

    //make sure the obj has properties, return nothing (or add exception handling) 
    if (properties.Length == 0) { return null; } 

    //it does so create the dataset and table 
    var dataSet = new DataSet(); 
    var dataTable = new DataTable(); 

    //now build the columns from the properties 
    var columns = new DataColumn[properties.Length]; 
    for (int i = 0; i < properties.Length; i++) 
    { 
      columns[i] = new DataColumn(properties[i].Name, properties[i].PropertyType); 
    } 

    //add columns to table 
    dataTable.Columns.AddRange(columns); 

    //now add the list values to the table 
    foreach (var item in list) 
    { 
      //create a new row from table 
      var dataRow = dataTable.NewRow(); 

      //now we have to iterate thru each property of the item and retrieve it's value for the corresponding row's cell 
      var itemProperties = item.GetType().GetProperties(); 

      for (int i = 0; i < itemProperties.Length; i++) 
      { 
       dataRow[i] = itemProperties[i].GetValue(item, null); 
      } 

      //now add the populated row to the table 
      dataTable.Rows.Add(dataRow); 
    } 

    //add table to dataset 
    dataSet.Tables.Add(dataTable); 

    //return dataset 
    return dataSet; 
} 

Come posso convertire il Elenco articoli in un'altra DataTable nel DataSet?

+0

provare questo mentre tornava dataset 'tornare dataset.GetXml();' –

risposta

5

Si può solo aggiungere un po 'di ricorsività al codice, qualcosa di simile:

var set = new DataSet(); 
AddToDataSet(set, resp); 

... 

public static void AddToDataSet(DataSet set, object value) 
{ 
    if (set == null) 
     throw new ArgumentNullException(nameof(set)); 

    if (value == null) 
     return; 

    var type = value.GetType(); 
    var table = set.Tables[type.FullName]; 
    if (table == null) 
    { 
     // create one table per type (name) 
     table = new DataTable(type.FullName); 
     set.Tables.Add(table); 
     foreach (var prop in type.GetProperties().Where(p => p.CanRead)) 
     { 
      if (IsEnumerable(prop)) 
       continue; 

      var col = new DataColumn(prop.Name, prop.PropertyType); 
      table.Columns.Add(col); 
     } 
    } 

    var row = table.NewRow(); 
    foreach (var prop in type.GetProperties().Where(p => p.CanRead)) 
    { 
     object propValue = prop.GetValue(value); 
     if (IsEnumerable(prop)) 
     { 
      if (propValue != null) 
      { 
       foreach (var child in (ICollection)propValue) 
       { 
        AddToDataSet(set, child); 
       } 
      } 
      continue; 
     } 

     row[prop.Name] = propValue; 
    } 
    table.Rows.Add(row); 
} 

private static bool IsEnumerable(PropertyInfo pi) 
{ 
    // note: we could also use IEnumerable (but string, arrays are IEnumerable...) 
    return typeof(ICollection).IsAssignableFrom(pi.PropertyType); 
}