C# 如何将字典转换为数据表?

C# 如何将字典转换为数据表?,c#,dictionary,datatable,C#,Dictionary,Datatable,使用C#,如何将字典转换为数据表 下面是一个关于如何将列表转换为数据表的示例 我从2019年12月17日开始研究这个 我还搜索了谷歌、Stackoverflow和微软的网站 public static DataTable DictionaryToDataTable<T>(this IDictionary<K, V> data) { PropertyDescriptorCollection propertiesK = TypeDescriptor.GetPrope

使用C#,如何将字典转换为数据表

下面是一个关于如何将列表转换为数据表的示例

我从2019年12月17日开始研究这个

我还搜索了谷歌、Stackoverflow和微软的网站

public static DataTable DictionaryToDataTable<T>(this IDictionary<K, V> data)
{
    PropertyDescriptorCollection propertiesK = TypeDescriptor.GetProperties(typeof(K));
    PropertyDescriptorCollection propertiesV = TypeDescriptor.GetProperties(typeof(V));

    DataTable table = new DataTable();

    foreach (PropertyDescriptor prop in propertiesK)
       table.Columns.Add(prop.Name,
                         Nullable.GetUnderlyingType(prop.PropertyType)
                         ?? prop.PropertyType);

    foreach (PropertyDescriptor prop in propertiesV)
        table.Columns.Add
              (prop.Name, Nullable.GetUnderlyingType(prop.PropertyType)  
                          ?? prop.PropertyType);

    foreach(KeyValuePair<K, V> item in data)
    {
       DataRow row = table.NewRow();

       foreach (PropertyDescriptor prop in propertiesK)
          row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;

       foreach (PropertyDescriptor prop in propertiesV)
          row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;

       table.Rows.Add(row);
    }

    return table;
}
公共静态数据表DictionaryDataTable(此IDictionary数据)
{
PropertyDescriptorCollection propertiesK=TypeDescriptor.GetProperties(typeof(K));
PropertyDescriptorCollection propertiesV=TypeDescriptor.GetProperties(typeof(V));
DataTable=新的DataTable();
foreach(propertiesK中的PropertyDescriptor属性)
表.列.添加(道具名称,
Nullable.GetUnderlineType(prop.PropertyType)
??道具属性类型);
foreach(propertiesV中的PropertyDescriptor属性)
table.Columns.Add
(prop.Name,null.getUnderlineType(prop.PropertyType)
??道具属性类型);
foreach(数据中的KeyValuePair项)
{
DataRow行=table.NewRow();
foreach(propertiesK中的PropertyDescriptor属性)
行[prop.Name]=prop.GetValue(项)??DBNull.Value;
foreach(propertiesV中的PropertyDescriptor属性)
行[prop.Name]=prop.GetValue(项)??DBNull.Value;
table.Rows.Add(行);
}
返回表;
}
Type ex=typeof(InformationInTransit.ProcessLogic.DataTableHelper);
MethodInfo mi=ex.GetMethod(“DictionaryToDataTable”);
MethodInfo微结构=
mi.MakeGenericMethod
(
类型
(
字典
<
一串
InformationInTransit.ProcessLogic.Exact.Participation
>
)
);
对象[]args={result};
DataTable=(DataTable)miConstructed.Invoke
(
无效的
args
);

一次尝试就是这样:

        Dictionary<int,int> dictionary=new Dictionary<int, int>();
        DataTable dt=new DataTable();
        dt.Columns.Add("Key", typeof(int));
        dt.Columns.Add("Val", typeof(int));

        foreach (var item in dictionary)
        {
            DataRow dr = dt.NewRow();
            dr["Key"] = item.Key;
            dr["Val"] = item.Value;
            dt.Rows.Add(dr);
        }
Dictionary Dictionary=newdictionary();
DataTable dt=新的DataTable();
添加(“键”,类型(int));
添加(“Val”,typeof(int));
foreach(字典中的变量项)
{
DataRow dr=dt.NewRow();
dr[“Key”]=项.键;
dr[“Val”]=项目值;
dt.Rows.Add(dr);
}
尝试一下:(名称列表和全局变量是列表)


请考虑向您的代码添加一些解释和细节。请您的问题,以澄清您的具体问题或添加额外的细节,以突出显示您所需要的。正如目前所写的,很难准确地说出你在问什么。请参阅页面以获取澄清此问题的帮助。
        Dictionary<int,int> dictionary=new Dictionary<int, int>();
        DataTable dt=new DataTable();
        dt.Columns.Add("Key", typeof(int));
        dt.Columns.Add("Val", typeof(int));

        foreach (var item in dictionary)
        {
            DataRow dr = dt.NewRow();
            dr["Key"] = item.Key;
            dr["Val"] = item.Value;
            dt.Rows.Add(dr);
        }
            var dictionary = nameList.Zip(globalVariable, (k, v) => new { Key = k, Value = v })
                                     .ToDictionary(x => x.Key, x => x.Value);

                DataTable dt = new DataTable();
                dt.Columns.Add("Variable names", typeof(string));
                dt.Columns.Add("Variable values", typeof(string));

                foreach (var pair in dictionary)
                {
                    DataRow dr = dt.NewRow();
                    dr["Variable names"] = pair.Key;
                    dr["Variable values"] = pair.Value;
                    dt.Rows.Add(dr);
                }