【问题标题】:Dictionary to DataTable with variable number of keys C#具有可变数量键的字典到 DataTable C#
【发布时间】:2020-06-23 12:49:51
【问题描述】:

我有一种方法,可以将字典列表转换为 DataTable。我还需要根据数据创建具有数据类型的列。字典中的某些值确实包含 null。我想出了以下几点:

    private static DataTable ToDataTable(List<Dictionary<string, object>> listOfDictionary)
    {
        var result = new DataTable();

        if (listOfDictionary != null && !listOfDictionary.Any())
        {
            return result;
        }

        var maxKeyCount = listOfDictionary.Select(x => x.Keys.Count).Max();

        var maxCountDictionary = listOfDictionary.Where(x => x.Keys.Count == maxKeyCount).FirstOrDefault();

        foreach (var column in maxCountDictionary.Select(c => new DataColumn(c.Key, c.Value == null ? typeof(string) : c.Value.GetType())))
        {
            result.Columns.Add(column);
        }

        listOfDictionary.ForEach(r => result.Rows.Add(r.Select(c => c.Value).Cast<object>().ToArray()));

        return result;
    }

如果列表中有两个字典,一个字典包含三个键,另一个包含四个键(最大为四个),那么数据表将有四列,并且数据行对于额外的列将具有 null/empty。

这段代码在这一行给出错误:

    listOfDictionary.ForEach(r => result.Rows.Add(r.Select(c => c.Value).Cast<object>().ToArray()));

用于尝试为额外的字典键添加 null/empty。

请提出建议。

【问题讨论】:

  • c.Value 给出了错误。所以试试:c =>(c == null)? null : c.Value
  • ok 让我试试上面的
  • @jdweng 或使用空条件c =&gt; c?.Value
  • 如果 c 为 null,它将没有值并给出异常。

标签: c# linq


【解决方案1】:
        static DataTable ToDataTable(List<Dictionary<string, object>> dictionaries)
    {
        var dataTable = new DataTable();
        if (dictionaries != null && dictionaries.Any())
        {
            var columns = GetColumns(dictionaries);
            foreach (var column in columns)
                dataTable.Columns.Add(column);

            var objectArrays = GetObjectArrays(dictionaries, dataTable.Columns);
            foreach (var array in objectArrays)
                dataTable.Rows.Add(array);
        }
        
        return dataTable;
    }

    static List<object[]> GetObjectArrays(List<Dictionary<string, object>> dictionaries, DataColumnCollection columns)
    {
        var arrays = new List<object[]>();
        foreach (var dictionary in dictionaries)
        {
            var rowValues = new object[columns.Count];
            foreach (var valuePair in dictionary)
                rowValues[columns.IndexOf(valuePair.Key)] = valuePair.Value;
            arrays.Add(rowValues);
        }

        return arrays;
    }

    static List<DataColumn> GetColumns(List<Dictionary<string, object>> dictionaries)
    {
        var columns = new List<DataColumn>();
        var dictWithMostKeys = GetDictionaryWithMostKeys(dictionaries);
        var dataColumns = GetDataColumns(dictWithMostKeys);
        foreach (var column in dataColumns)
            columns.Add(column);

        return columns;
    }


    static IEnumerable<DataColumn> GetDataColumns(Dictionary<string, object> dictionary)
    {
        return dictionary.Select(c => new DataColumn(c.Key, c.Value == null ? typeof(string) : c.Value.GetType()));
    }
    static Dictionary<string, object> GetDictionaryWithMostKeys(List<Dictionary<string, object>> dictionaries)
    {
        var maxKeySum = dictionaries.Select(dictionary => dictionary.Count).Max();
        return dictionaries.First(dictionary => dictionary.Count == maxKeySum);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2019-12-25
    相关资源
    最近更新 更多