是否有更有效的(首选单行代码但不是必需的)方法来设置数据表列结构,而不是像我下面的 foreach?
// Identifiers used are:
var dataTable = new DataTable();
var columns = new System.Collections.Generic.Dictionary<string, System.Type>()
{
{ "Column 1", typeof(int) },
{ "Column 2", typeof(string) },
{ "Column 3", typeof(string) }
};
foreach (System.Collections.Generic.KeyValue<string, System.Type> column in columns)
{
dataTable.Columns.Add(column.Key, column.Value);
}
最佳答案
您可以使用 LINQ。
columns.ToList().ForEach(c => dataTable.Columns.Add(c.Key, c.Value))
基本上是一样的。
关于c# - 如何使用列名字典来设置数据表结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51635515/