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

  • Home
  • SEARCH
  • 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 8104053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:43:36+00:00 2026-06-05T23:43:36+00:00

I need to loop through a List of Dictionaries List<Dictionary<string,string>> to populate a DataTable.

  • 0

I need to loop through a List of Dictionaries

List<Dictionary<string,string>> 

to populate a DataTable. Each Dictionary in the list has a Key, which needs to be the column name, and a Value which is what’s in that column. The list contains 225 dictionaries (225 rows to the table).

List<Dictionary<string, string>> myList = 
       JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(jsonRep);
DataTable dt = new DataTable();

    //loop through list, loop through dictionaries, add keys as columns, 
    //values as rows.                     

so far, I have been trying..

//get max columns
int columns = myList[0].Count; <--gives me 13
//add columns
for (int i = 0; i < columns; i++)
   dt.Columns.Add(string myList[i].Keys);  <--somehow get to the key in dict to add as column names         
//add rows
foreach (var x in myList)
{
     dt.Rows.Add(x); <--not working
}
jsonReprValue = dt; <--save new DataTable to var jsonReprValue

How do I do this correctly?
Thanks!

  • 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-06-05T23:43:37+00:00Added an answer on June 5, 2026 at 11:43 pm

    You have two problems. One is adding the columns, and the other is adding the rows.

    Adding the columns

    Assuming your list has items in it, and that all the dictionaries in the list have the same keys, then you only need to add columns from one of the dictionaries:

    foreach(string column in myList[0].Keys)
    {
        dt.Columns.Add(column);
    }
    

    Adding the Rows

    Change this:

    foreach (var x in myList)
    {
        dt.Rows.Add(x); <--not working
    }
    

    To this:

    foreach(Dictionary<string, string> dictionary in myList)
    {
        DataRow dataRow = dt.NewRow();
    
        foreach(string column in dictionary.Keys)
        {
            dataRow[column] = dictionary[column];
        }
    
        dt.Rows.Add(dataRow);
    }
    

    See DataTable.NewRow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My controller is passing through a list which I then need to loop through
I have a list of words, in Python I need to loop through each
I need to loop through each day of the week (monday tues...) and compare
I have a foreach loop in jQuery going through each 'div.panel', I need to
I have a for loop to loop through my list of SliderItems which contain
I need to loop through my django list that I have passed to the
I need to loop through categories and then the top stores within each of
I need to loop through a list of fields in a custom validator to
I need to loop through a dictionary.. backwards from the normal foreach.. loop. What
I need to loop through an entire list of users, but need to grab

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.