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 8532481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:43:31+00:00 2026-06-11T09:43:31+00:00

I have an advanced program that retrieves data from a database and creates objects

  • 0

I have an advanced program that retrieves data from a database and creates objects from the data via ExpandoObjects in C#.

I’m now in the process of optimizing my entire process but came upon a piece of code that was the bottleneck performance-wise. I am quite curious how far I can furhter optimize this piece of code and already managed to run it 3 times as fast by doing the following:

  • Seperated ‘the finding ‘types’ section’ in a seperate for loop and only iterate over it when it hasn’t been initialized yet.
  • Added a case for when the value is null and create an empty string instead, since for some reason Dictionary.Add slows down quite a lot when adding Null values.

        // Holds all objects that are created inside the object.
        Dictionary<string, IDictionary<string, dynamic>> objects = new Dictionary<string, IDictionary<string, dynamic>>();
    
        // This foreach loop is the slowest part!
        foreach (KeyValuePair<string, dynamic> pair in fields)
        {
            string type = pair.Key.Split('_')[0];
    
            IDictionary<string, dynamic> obj;
            if (!objects.TryGetValue(type, out obj))
            {
                obj = new ExpandoObject();
                objects.Add(type, obj);
            }
            int location = pair.Key.IndexOf(type + "_");
            string key = pair.Key.Remove(location, type.Length + 1);
            if (pair.Value == null)         // If Value is null, replace it with an empty string (dictionary slows quite alot when passing it null values)
                obj.Add(key, "");
            else
                obj.Add(key, pair.Value);         
        }
    
        if (types == null)
            types = objects.Select(x => x.Key).ToList();
    

I am wondering, how is it that dictionary slows down that much when adding Null values, is it that in the underlying structure it does special operations when encountering null values? And is there something I’m missing to further optimize the code?.

Any help is again greatly appreciated.


UPDATE

  • Edited the code with the most recent changes I’ve gathered from SO.
  • 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-11T09:43:33+00:00Added an answer on June 11, 2026 at 9:43 am

    You can avoid the multiple lookups in the dictionary by using TryGetValue instead of ContainsKey:

    foreach (KeyValuePair<string, dynamic> pair in fields) 
    { 
        string type = pair.Key.Split('_')[0]; 
    
        IDictionary<string, dynamic> obj;
        if (!objects.TryGetValue(type, out obj)) 
        { 
            obj = new ExpandoObject(); 
            objects.Add(type, obj); 
        } 
        int location = pair.Key.IndexOf(type + "_"); 
        string key = pair.Key.Remove(location, type.Length + 1); 
        if (pair.Value == null)
            obj.Add(key, ""); 
        else 
            obj.Add(key, pair.Value);            
    } 
    

    BTW: I don’t see you using types in that second foreach loop. Therefore you can replace the first foreach loop with this simple code:

    types = objects.Select(x => x.Key).ToList();
    

    This should save quite a lot of time as it removes the slow search in the types list and the double parsing of the types.

    Obviously this needs to be put after the foreach loop I showed above.

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

Sidebar

Related Questions

I have a C# program that retrieves multiple rows from a DB but currently
I have a C# program that works with an SQL Server database. The setup
I have created a database program where I am able to add the data
I have 2 advanced data grids, one above the other. They both have the
I currently have an advanced search page that searches a number of fields in
BackGround: I have an advanced data grid. The data provider for this ADG is
I have a reasonably advanced (many patches and subpatches) quartz composition that was created
I have a little sketch program that I've managed to throw together using Quartz2D,
I have this code right here to retrive the IP-address from a hostname: program
Simple case I have a Python program that I intend to support on both

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.