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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:22:38+00:00 2026-05-25T12:22:38+00:00

I just want to ask if: The code below is efficient? Is there a

  • 0

I just want to ask if:

  1. The code below is efficient?
  2. Is there a better way to handle this?
  3. How to code if additional values for tablename/fieldname pair are needed?

We need to use a multi-key dictionary that contains something like (TableName, FieldName, FieldValue).

I searched some answer but the ones I found so far are not applicable to our setup. We are using 3.5 so no Tuple available yet. We are also integrating this script logic with an application that only allows coding “inside” a method body, so we are limited and cannot create a separate class/structure, etc. Our set up is C#/VS 2010.

Any help is appreciated. Thanks in advance!

Dictionary<string, Dictionary<string, string>> tableList = new Dictionary<string, Dictionary<string, string>>();
Dictionary<string, string> fieldList = new Dictionary<string, string>();

// add fields to field list, then add the field lists to the corresponding table list
// clear field list for next table
// values are just hardcoded here to simplify, but is being read from actual objects in the application

fieldList.Add("Field1", "abc");
fieldList.Add("Field2", "def");
fieldList.Add("Field3", "ghi");
fieldList.Add("Field4", "jkl");

tableList.Add("Table1", new Dictionary<string, string>(fieldList));
fieldList.Clear();

fieldList.Add("Field1", "xyz");
fieldList.Add("Field2", "uvw");
fieldList.Add("Field3", "rst");

tableList.Add("Table2", new Dictionary<string, string>(fieldList));
fieldList.Clear();

fieldList.Add("Field1", "123");
fieldList.Add("Field2", "456");

tableList.Add("Table3", new Dictionary<string, string>(fieldList));
fieldList.Clear();

// Display tables and corresponding fields                          

foreach (KeyValuePair<string, Dictionary<string, string>> fieldList4 in tableList)
{
    foreach (KeyValuePair<string, string> fieldList5 in fieldList4.Value)
    {
        txtMessage.Text = txtMessage.Text + "\r\nTable=" + fieldList4.Key + ", Field=" + fieldList5.Key + " - " + fieldList5.Value;
    }
}

// Try to find tables and fields in the lists, and list the value if found

string tableToFind = "Table2";
string fieldToFind = "Field2";
Dictionary<string, string> tableFields = new Dictionary<string, string>();

if (tableList.Keys.Contains(tableToFind) == true)
{
    txtMessage.Text = txtMessage.Text = "\r\nTable=" + tableToFind + " exist in table list";
    tableList.TryGetValue(tableToFind, out tableFields);

    if (tableFields.Keys.Contains(fieldToFind) == true)
    {
        foreach(KeyValuePair<string, string> fieldData  in tableFields)
        {
            if (fieldData.Key == fieldToFind)
            {
                txtMessage.Text = txtMessage.Text + "\r\nTable=" + tableToFind + ", Field=" +  fieldData.Key + 
                                                    " with value=" + fieldData.Value + " exist in table list";
                break;
            }
        }
    }
}
  • 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-25T12:22:38+00:00Added an answer on May 25, 2026 at 12:22 pm

    There’s a whole slew of problems and inefficiencies in your code.

    1. If you’re going to create multiple dictionaries, create the dictionaries directly. Don’t use a separate instance to fill the values and copy from.

    2. Never use string concatenation in a loop like that. Use a StringBuilder or other similar mechanism to build up your strings. You already have your values in a collection so using String.Join() in conjunction with LINQ would clean that up.

    3. Your approach to get values from the dictionary is awkward to say the least. Normally you’d use TryGetValue() alone to attempt to read the key. Your code uses it incorrectly. If you are going to check if the key exists in the dictionary (using Contains()), then there’s no point in using TryGetValue(). To make things worse, you did this then searched for the key manually in the inner dictionary by iterating through the key value pairs.

    The typical pattern looks like this:

    DictValueType value;
    if (myDict.TryGetValue(key, out value))
    {
        // key was in the dictionary, the value is stored in the `value` variable
    }
    

    The code you have could be written much much more efficiently like this:

    var tableList = new Dictionary<string, Dictionary<string, string>>
    {
        { "Table1", new Dictionary<string, string>
                    {
                        { "Field1", "abc" },
                        { "Field2", "def" },
                        { "Field3", "ghi" },
                        { "Field4", "jkl" },
                    }
        },
        { "Table2", new Dictionary<string, string>
                    {
                        { "Field1", "xyz" },
                        { "Field2", "uvw" },
                        { "Field3", "rst" },
                    }
        },
        { "Table3", new Dictionary<string, string>
                    {
                        { "Field1", "123" },
                        { "Field2", "456" },
                    }
        },
    };
    
    // Display tables and corresponding fields
    txtMessage.Text = String.Join("\r\n",
        tableList.SelectMany(table =>
            table.Value.Select(fieldList =>
                String.Format("Table={0}, Field={1} - {2}",
                    table.Key, fieldList.Key, fieldList.Value)
            )
        ).ToArray()
    );
    
    // (I hope you have this in a separate method)
    // Try to find tables and fields in the lists, and list the value if found
    string tableToFind = "Table2";
    string fieldToFind = "Field2";
    
    var builder = new StringBuilder(txtMessage.Text); // mostly useful if you have a 
                                                      // lot of different strings to add
    Dictionary<string, string> foundTable;
    if (tableList.TryGetValue(tableToFind, out foundTable))
    {
        builder.AppendLine()
            .Append("Table=" + tableToFind + " exist in table list");
    
        string foundField;
        if (foundTable.TryGetValue(fieldToFind, out foundField))
        {
            builder.AppendLine()
                .AppendFormat("Table={0}, Field={1} with value={2} exist in table list",
                    tableToFind, fieldToFind, foundField);
        }
    }
    txtMessage.Text = builder.ToString();
    

    Nested dictionaries aren’t a bad thing, it’s a nice way to organize hierarchies of keys and values. But to keep it maintainable, you generally should encapsulate everything within another class providing methods to manipulate the data without having to manage the dictionaries directly. You can make it both efficient and maintainable. How to implement this is an exercise left to you.

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

Sidebar

Related Questions

I just want to ask something about my code. #define LIM 40 main() {
just want to ask anyone here can solve this problem? I want to create
I just want to ask how should I put my image (dynamically) in this
I just want to ask for your experience. I'm designing a public website, using
I don't know too much about encryption, I just want to ask, which method
Just want to know if there other optional wrapper framework available that generate client
I just want a quick way (and preferably not using a while loop)of createing
(For just multi-line strings in Java, see: Java multiline string . This question below
Can anyone beat the performance of my integer to std::string code, linked below? There
In the code below, I cannot figure out a way of passing a member

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.