Note that there are some explanatory texts on larger screens.

plurals
  1. POExtension method to get list of dictionaries as dataset?
    primarykey
    data
    text
    <p>I'm trying to cast a List of Dictionary objects to a dataset. The List comes from a JSON parser. I decided to use this as an opportunity to learn about extension methods.</p> <p>The extension method for a single dictionary works, but the method for a List of Dictionaries doesn't "look" right to me, mainly because the call becomes</p> <pre><code>DataSet myExampleDataSet = myExampleDictionary.ToDataSet&lt;Dictionary&lt;string,string&gt;,string,string&gt;(); </code></pre> <p>Am I missing something? Does it really have to be this complicated? Should I just throw the Dictionary .ToDataSet method in a foreach?</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Collections; //fixed code below namespace TT.Utils { public static class DictionaryExtensions { /// &lt;summary&gt; /// Dictionary to DataSet /// &lt;/summary&gt; /// &lt;typeparam name="TKey"&gt;&lt;/typeparam&gt; /// &lt;typeparam name="TValue"&gt;&lt;/typeparam&gt; /// &lt;param name="currentDictionary"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static DataSet ToDataSet&lt;TKey, TValue&gt;(this IDictionary&lt;TKey, TValue&gt; currentDictionary) { DataSet exportedDataSet = new DataSet(); DataTable exportedDataTable = exportedDataSet.Tables.Add(); foreach (TKey key in currentDictionary.Keys) { exportedDataTable.Columns.Add(key.ToString()); } DataRow newRow = exportedDataTable.NewRow(); foreach (KeyValuePair&lt;TKey, TValue&gt; entry in currentDictionary) { string key = entry.Key.ToString(); string val = string.Empty; if (entry.Value != null) { val = entry.Value.ToString(); } newRow[key] = val; } exportedDataSet.Tables[0].Rows.Add(newRow); return exportedDataSet; } /// &lt;summary&gt; /// List of dictionaries to dataset /// &lt;/summary&gt; /// &lt;typeparam name="TKey"&gt;&lt;/typeparam&gt; /// &lt;typeparam name="TValue"&gt;&lt;/typeparam&gt; /// &lt;param name="currentList"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static DataSet ToDataSet&lt;TKey,TValue&gt;(this IList&lt;Dictionary&lt;TKey,TValue&gt; currentList) { DataSet exportedDataSet = new DataSet(); DataTable exportedDataTable = exportedDataSet.Tables.Add(); foreach (Dictionary&lt;TKey, TValue&gt; currentDictionary in currentList.Cast&lt;Dictionary&lt;TKey,TValue&gt;&gt;()) { foreach (TKey key in currentDictionary.Keys) { if (!exportedDataTable.Columns.Contains(key.ToString())) exportedDataTable.Columns.Add(key.ToString()); } DataRow newRow = exportedDataTable.NewRow(); foreach (KeyValuePair&lt;TKey, TValue&gt; entry in currentDictionary) { string key = entry.Key.ToString(); string val = string.Empty; if (entry.Value != null) { val = entry.Value.ToString(); } newRow[key] = val; } exportedDataSet.Tables[0].Rows.Add(newRow); } return exportedDataSet; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload