当使用 expando 对象填充我的数据表时,我在初始化一个新的datarow
. 例外是:
无效的存储类型:DBNull
public static DataTable ToCLDataTable<T>(List<T> items)
{
DataTable dataTable = new DataTable("CLWorkQueue");
//Get all the properties
DataRow dr;
var expandoDict = items[0] as IDictionary<string, object>;
foreach (var key in expandoDict.Keys)
{
if ((expandoDict[key]).GetType() == typeof(DateTime))
{
dataTable.Columns.Add(key.ToString(), typeof(DateTime));
}
else
{
dataTable.Columns.Add(key.ToString(), expandoDict[key].GetType());
}
}
for (int i = 0; i < items.Count; i++)
{
var expandoDictData = items[i] as IDictionary<string, object>;
//var values = new object[expandoDictData.Count];
int j = 0;
dr = dataTable.NewRow(); /*Though the datatable has all the required columns, but whenever i try to initialize a new row an exception occurs.*/
foreach (var key in expandoDictData.Keys)
{
//values[j] = expandoDictData[key].ToString();
dr[key] = expandoDictData[key].ToString();
j++;
}
dataTable.Rows.Add(dr);
//dataTable.Rows.Add(values);
}[enter image description here][1]