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
的属性,但是我一直无法弄清楚如何填充 Datatable
当List<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");
使用您的示例进行测试,它会产生所需的结果:
以上代码使用了 .NET 4.5 中引入的 PropertyInfo.GetMethod
和 PropertyInfo.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/