如何在 C# 中将嵌套列表转换为数据集 [英] How to convert nested List to Dataset in C#

查看:15
本文介绍了如何在 C# 中将嵌套列表转换为数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套列表,我需要将它转换为 C# 中的 DataSet.我发现了很多关于这个的例子,但他们没有做我需要的.我在列表中有一个列表,我需要 DataSet 中另一个 DataTable 中的嵌套列表.

I have a nested list and I need to convert it to DataSet in C#. I found many example about this but they dont do what I need. I have a list in a list, I need the nested list in another DataTable in the DataSet.

这是一个列表的例子

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

我使用下面的代码将 List 转换为 DataSet,但它没有将 Items 转换为 DataSet 中的 DataTable

I have used the code below to convert the List to DataSet but It didnt convert Items to DataTable in DataSet

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;
}

如何将 Items 列表转换为另一个 DataTable 到 DataSet 中?

How can I convert the Items list to another DataTable into the DataSet?

推荐答案

你可以在你的代码中添加一点递归,像这样:

You can just add a bit of recursivity to your code, something like this:

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

这篇关于如何在 C# 中将嵌套列表转换为数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆