locked
Update a value in a Dictionary using loop RRS feed

  • Question

  • Hi,

      i have requirement to update the dictionary values using loop. and below is code am using

      Dictionary<string, object> dictionary = Directory.GetFiles(txtSource.Text, "*.*").Where(s => ".txt".Contains(Path.GetExtension(s).ToLower())).ToDictionary(s => s.ToString(), s => (object)s);

     List<KeyValuePair<string, object>> list = new List<KeyValuePair<string, object>>(dictionary);                       
                                DataTable dt ;
                                foreach (KeyValuePair<string, object> kvp in list)
                                {
                                    dt = GetFileDetail(kvp.Value);

                                    dictionary[kvp.Key]=dt;
                                }

    Problem is Instead of updated value to corresponding key.  its updating entire dictionary  value with last GetFileDetail result.

    any idea?




    Thursday, December 13, 2012 10:03 AM

Answers

  • Am facing the problem with DataTable

    Here is my side every thing works fine even for DataTable . Here is the sample code :

     public void TestMethod()
            {
                Dictionary<string, object> dictionary = new Dictionary<string, object>();
                dictionary["key1"] = 10;
                dictionary["key2"] = 20;
                dictionary["key3"] = 30;
    
                //Directory.GetFiles(txtSource.Text, "*.*")
                //.Where(s => ".txt".Contains(Path.GetExtension(s).ToLower())).ToDictionary(s => s.ToString(), s => (object)s);
    
                List<KeyValuePair<string, object>> list = new List<KeyValuePair<string, object>>(dictionary);
                DataTable dt;
                string x;
                foreach (KeyValuePair<string, object> kvp in list)
                {
                    dt = GetFileDetail(kvp.Value);
                    // x = kvp.Value.ToString()+ " new data";
                    dictionary[kvp.Key] = dt;
                }
            }
    
            public DataTable GetFileDetail(object x)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Col1");
                dt.Columns.Add("Col2");
                DataRow dr = dt.NewRow();
                dr[0] = x;
                dr[1] = "Data added";
                dt.Rows.Add(dr);
                return dt;
            }


    One good question is equivalent to ten best answers.

    • Proposed as answer by Mike Feng Friday, December 14, 2012 9:25 AM
    • Marked as answer by Mike Feng Tuesday, December 18, 2012 12:52 PM
    Thursday, December 13, 2012 12:39 PM
  • Why are you processing the loop that way?

    Easier would be this:

    //process each val in your dic and save back

                                foreach (var key in dictionary.Keys)
                                {

                                    dictionary[key]= GetFileDetail(dictionary[key]);
                                }

    Note; Didn't syntax check in VS, but thats the gist.

    Honestly, your code could be even cleaner.

    Dictionary<string, object> dictionary = Directory.GetFiles(txtSource.Text, "*.*").
    Where(s => ".txt".
    |Contains(Path.GetExtension(s).ToLower())).
    ToDictionary(s => s.ToString(), s => GetFileDetail(s));

    Now it is all one step.

    • Proposed as answer by Mike Feng Friday, December 14, 2012 9:25 AM
    • Marked as answer by Mike Feng Tuesday, December 18, 2012 12:52 PM
    Thursday, December 13, 2012 4:42 PM

All replies

  • Hi,

    I cant understand ur scenario. But try the below code instead of ur part of code and it may works for you.

     DataTable dt ;
     foreach (KeyValuePair<string, object> kvp in list)
     {
      dt=new DataTable();
      dt = GetFileDetail(kvp.Value);
      dictionary[kvp.Key]=dt;
     }


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"

    Thursday, December 13, 2012 11:22 AM
  • Hi, but here on my side everything is perfect .

    I just created a sample codes for this . It is working fine here .

    Dictionary<string, object> dictionary = new Dictionary<string, object>();
                dictionary["key1"] = 10;
                dictionary["key2"] = 20;
                dictionary["key3"] = 30;
    
                    //Directory.GetFiles(txtSource.Text, "*.*")
                    //.Where(s => ".txt".Contains(Path.GetExtension(s).ToLower())).ToDictionary(s => s.ToString(), s => (object)s);
    
                List<KeyValuePair<string, object>> list = new List<KeyValuePair<string, object>>(dictionary);
                DataTable dt;
                string x;
                foreach (KeyValuePair<string, object> kvp in list)
                {
                   // dt = GetFileDetail(kvp.Value);
                    x = kvp.Value.ToString()+ " new data";
                    dictionary[kvp.Key] = x;
                }
    I have commented your code and substituted some simple codes similar to that. It is working fine.All values of corresponding keys are updated .Can you test it on your side , and let me know !


    One good question is equivalent to ten best answers.

    Thursday, December 13, 2012 11:45 AM
  • No Luck.. still same issues.. Problem is after the end of Loop .. all  the value are updated the with last FetFileDetail Result. Means if Dictionary contain 100 rows. all 100 row are  updated with same value i.e Last value of GetFileDetail
    Thursday, December 13, 2012 12:09 PM
  • Am facing the problem with DataTable
    Thursday, December 13, 2012 12:10 PM
  • Am facing the problem with DataTable

    Here is my side every thing works fine even for DataTable . Here is the sample code :

     public void TestMethod()
            {
                Dictionary<string, object> dictionary = new Dictionary<string, object>();
                dictionary["key1"] = 10;
                dictionary["key2"] = 20;
                dictionary["key3"] = 30;
    
                //Directory.GetFiles(txtSource.Text, "*.*")
                //.Where(s => ".txt".Contains(Path.GetExtension(s).ToLower())).ToDictionary(s => s.ToString(), s => (object)s);
    
                List<KeyValuePair<string, object>> list = new List<KeyValuePair<string, object>>(dictionary);
                DataTable dt;
                string x;
                foreach (KeyValuePair<string, object> kvp in list)
                {
                    dt = GetFileDetail(kvp.Value);
                    // x = kvp.Value.ToString()+ " new data";
                    dictionary[kvp.Key] = dt;
                }
            }
    
            public DataTable GetFileDetail(object x)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Col1");
                dt.Columns.Add("Col2");
                DataRow dr = dt.NewRow();
                dr[0] = x;
                dr[1] = "Data added";
                dt.Rows.Add(dr);
                return dt;
            }


    One good question is equivalent to ten best answers.

    • Proposed as answer by Mike Feng Friday, December 14, 2012 9:25 AM
    • Marked as answer by Mike Feng Tuesday, December 18, 2012 12:52 PM
    Thursday, December 13, 2012 12:39 PM
  • Why are you processing the loop that way?

    Easier would be this:

    //process each val in your dic and save back

                                foreach (var key in dictionary.Keys)
                                {

                                    dictionary[key]= GetFileDetail(dictionary[key]);
                                }

    Note; Didn't syntax check in VS, but thats the gist.

    Honestly, your code could be even cleaner.

    Dictionary<string, object> dictionary = Directory.GetFiles(txtSource.Text, "*.*").
    Where(s => ".txt".
    |Contains(Path.GetExtension(s).ToLower())).
    ToDictionary(s => s.ToString(), s => GetFileDetail(s));

    Now it is all one step.

    • Proposed as answer by Mike Feng Friday, December 14, 2012 9:25 AM
    • Marked as answer by Mike Feng Tuesday, December 18, 2012 12:52 PM
    Thursday, December 13, 2012 4:42 PM