Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3350184
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:45:21+00:00 2026-05-18T01:45:21+00:00

I’m trying to cast a List of Dictionary objects to a dataset. The List

  • 0

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.

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

DataSet myExampleDataSet = myExampleDictionary.ToDataSet<Dictionary<string,string>,string,string>();

Am I missing something? Does it really have to be this complicated? Should I just throw the Dictionary .ToDataSet method in a foreach?

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
    {
        /// <summary>
        /// Dictionary to DataSet
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="currentDictionary"></param>
        /// <returns></returns>
        public static DataSet ToDataSet<TKey, TValue>(this IDictionary<TKey, TValue> 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<TKey, TValue> 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;
        }

        /// <summary>
        /// List of dictionaries to dataset
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="currentList"></param>
        /// <returns></returns>
        public static DataSet ToDataSet<TKey,TValue>(this IList<Dictionary<TKey,TValue> currentList)
        {
            DataSet exportedDataSet = new DataSet();
            DataTable exportedDataTable = exportedDataSet.Tables.Add();



            foreach (Dictionary<TKey, TValue> currentDictionary in currentList.Cast<Dictionary<TKey,TValue>>())
            {
                foreach (TKey key in currentDictionary.Keys)
                {
                    if (!exportedDataTable.Columns.Contains(key.ToString()))
                        exportedDataTable.Columns.Add(key.ToString());
                }
                DataRow newRow = exportedDataTable.NewRow();
                foreach (KeyValuePair<TKey, TValue> 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;
        }



    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:45:22+00:00Added an answer on May 18, 2026 at 1:45 am

    You generally don’t need to explicitly define the generic type arguments used when calling a generic method. The types will be implied by the types of the arguments you are calling with. If there’s an ambiguity, the compiler will let you know.

    e.g.,

    IDictionary<string, string> myExampleDictionary = ...;
    DataSet myExampleDataSet = myExampleDictionary.ToDataSet();
    // equivalent to:          myExampleDictionary.ToDataSet<string, string>();
    
    IDictionary<string, int> anotherDictionary = ...;
    DataSet anotherDataSet = anotherDictionary.ToDataSet();
    // equivalent to:        anotherDictionary.ToDataSet<string, int>();
    

    Oops, I didn’t realize you overloaded ToDataSet(). I only saw the first one which took a single IDictionary<TKey, TValue>.

    But either way, the second overload should have the generic parameters the same as the first. It will be a IList of IDictionarys so it should be in this form:

    public static DataSet ToDataSet<TKey, TValue>(this IList<IDictionary<TKey, TValue>> currentList)
    {
        // ...
    }
    

    There are only 2 unknown types here, TKey and TValue and that will be implied as usual.

    IList<IDictionary<string, string>> myList = ...;
    DataSet myDataSet = myList.ToDataSet();
    // equivalent to:   myList.ToDataSet<string, string>();
    
    IDictionary<string, int> anotherList = ...;
    DataSet anotherDataSet = anotherList.ToDataSet();
    // equivalent to:        anotherList.ToDataSet<string, int>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.