c# - 将通用嵌套列表转换为数据表

标签 c# list generics datatable

Continuation from "Convert generic List/Enumerable to DataTable?"

我一直在使用以下代码来隐藏通用 List<T>进入DataTable :

    public static DataTable ToDataTable<T>(this IList<T> data)
    {
        PropertyDescriptorCollection properties = 
            TypeDescriptor.GetProperties(typeof(T));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (T item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                 row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;
    }

但是,我现在有一个列表 List<foo>哪里foo包含一个属性 List<bar> (其中 List<bar> 可以包含零值)。

问题:

我想转换通用 List<foo>包含另一个泛型嵌套 List<bar>例如:

        List<bar> GenericNestedList = new List<bar>{ new bar("barA"), new bar("barB") };
        List<foo> GenericList = new List<foo> { new foo("fooA", GenericNestedList) };

结果是 DataTable :

| GenericProperty | GenericNestedProperty |
|-----------------|-----------------------|
|      fooA       |         barA          |
|      fooA       |         barB          |

原始代码解释了foo内的许多属性。 , bar也会有这个要求。

到目前为止,我已经能够检索 bar 的属性,但是我一直无法弄清楚如何填充 DatatableList<bar>是空的。我没有太多编写泛型方法的经验,所以我很抱歉无法提供很多我的工作。

其他示例:

列表

List<foo> GenericList = new List<foo> { new foo("fooB", new List<bar>()) };

数据表

| GenericProperty | GenericNestedProperty |
|-----------------|-----------------------|
|      fooB       |                       |

列表

        List<bar> GenericNestedListA = new List<bar>{ new bar("barC"), new bar("barD") };
        List<bar> GenericNestedListB = new List<bar> { new bar("barE"), new bar("barF") };

        List<foo> GenericList = new List<foo> { new foo("fooC", GenericNestedListA),
                                                new foo("fooD", GenericNestedListB) };

数据表

| GenericProperty | GenericNestedProperty |
|-----------------|-----------------------|
|      fooC       |         barC          |
|      fooC       |         barD          |
|      fooD       |         barE          |
|      fooD       |         barF          |

类(class):

foo

class foo
{
    public string GenericProperty;
    public List<bar> GenericNestedList;

    public foo(string GenericProperty, List<bar> GenericNestedList)
    {
        this.GenericProperty = GenericProperty;
        this.GenericNestedList = GenericNestedList;
    }
}

class bar
{
    public string GenericNestedProperty;

    public bar(string GenericNestedProperty)
    {
        this.GenericNestedProperty = GenericNestedProperty;
    }
}

最佳答案

只是一个快速解决方案:

public DataTable CreateNestedDataTable<TOuter, TInner>(IEnumerable<TOuter> list, string innerListPropertyName)
{
    PropertyInfo[] outerProperties = typeof(TOuter).GetProperties().Where(pi => pi.Name != innerListPropertyName).ToArray();
    PropertyInfo[] innerProperties = typeof(TInner).GetProperties();
    MethodInfo innerListGetter = typeof(TOuter).GetProperty(innerListPropertyName).GetMethod;

    // set up columns
    DataTable table = new DataTable();
    foreach (PropertyInfo pi in outerProperties)
        table.Columns.Add(pi.Name, Nullable.GetUnderlyingType(pi.PropertyType) ?? pi.PropertyType);
    foreach (PropertyInfo pi in innerProperties)
        table.Columns.Add(pi.Name, Nullable.GetUnderlyingType(pi.PropertyType) ?? pi.PropertyType);

    // iterate through outer items
    foreach (TOuter outerItem in list)
    {
        var innerList = innerListGetter.Invoke(outerItem, null) as IEnumerable<TInner>;
        if (innerList == null || innerList.Count() == 0)
        {
            // outer item has no inner items
            DataRow row = table.NewRow();
            foreach (PropertyInfo pi in outerProperties)
                row[pi.Name] = pi.GetValue(outerItem) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        else
        {
            // iterate through inner items
            foreach (object innerItem in innerList)
            {
                DataRow row = table.NewRow();
                foreach (PropertyInfo pi in outerProperties)
                    row[pi.Name] = pi.GetValue(outerItem) ?? DBNull.Value;
                foreach (PropertyInfo pi in innerProperties)
                    row[pi.Name] = pi.GetValue(innerItem) ?? DBNull.Value;
                table.Rows.Add(row);
            }
        }
    }

    return table;
}

人们可能可以进一步扩展它,这样它就可以使用多个嵌套列表,或者自动识别嵌套列表的属性。但我在这里保持简单。

它的用法如下:

var table = CreateNestedDataTable<foo, bar>(GenericList, "GenericNestedList");

使用您的示例进行测试,它会产生所需的结果:

Desired results


以上代码使用了 .NET 4.5 中引入的 PropertyInfo.GetMethodPropertyInfo.GetValue。对于 4.0,进行以下替换:

// before
typeof(TOuter).GetProperty(innerListPropertyName).GetMethod;

// after
typeof(TOuter).GetProperty(innerListPropertyName).GetGetMethod(true);


// for each row assignment
// before
row[pi.Name] = pi.GetValue(item) ?? DBNull.Value;

// after
row[pi.Name] = pi.GetValue(item, null) ?? DBNull.Value;

关于c# - 将通用嵌套列表转换为数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33823820/

相关文章:

c# - javascript 中 DateTime.ToOADate() 的等价物是什么?

java - 方法返回类型前的 <T> 是什么意思?

c# - 尝试调用 function<T> where T : TheClass, new() where T: class

java - 泛型方法的子类实现

c# - 使用 MySQL 代替 LocalDb

c# - DTO POCO转换

c# - 在堆上查找最接近给定地址的 .NET 对象 (.NET2.0/3.5)

c - 链接列表错误(语言: C)

c# - List<T> 被清除问题

python - 如何每行打印列表中的 X 个项目