Click here to Skip to main content
15,896,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
int column = 43;
foreach (DataRow row in dt.Rows)
       {
   var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(row["farm_detailsdata"].ToString());

           string str = string.Empty;

                 foreach (var data in dict)
                     {
                  columni++;
          worksheet.Cells[2, columnindex].Value = data.Key;
                      }
            }

from the above how to add dicationary values using list

here in this below line dictionary value is there
C#
foreach (var data in dict)

how to add dictionary value to list in c#

from my above how to add dictionary value to list in c#

What I have tried:

C#
int column = 43;
foreach (DataRow row in dt.Rows)
       {
   var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(row["farm_detailsdata"].ToString());

           string str = string.Empty;

                 foreach (var data in dict)
                     {
                  columni++;
          worksheet.Cells[2, columnindex].Value = data.Key;
                      }
            }

from the above how to add dicationary values using list

here in this below line dictionary value is there
C#
foreach (var data in dict)

how to add dictionary value to list in c#

from my above how to add dictionary value to list in c#
Posted
Updated 8-May-18 5:38am
v3

1 solution

The simplest way is to use List(T).AddRange Method (IEnumerable(T)) (System.Collections.Generic)[^] . See:
C#
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "A");
dict.Add(2, "B");
dict.Add(3, "C");
dict.Add(4, "D");
dict.Add(5, "E");

List<string> lst = new List<string>();
lst.AddRange(dict.Values); //here!


You can use Dictionary.ToList(TSource) Method (IEnumerable(TSource)) (System.Linq)[^] too to "convert" dictionary object into list:
C#
List<KeyValuePair<int, string>> lst = dict.ToList();


For values only:
List<string> lst = dict.Values.ToList();


For further details, pleas see:
List(T) Class (System.Collections.Generic)[^]
Dictionary(TKey, TValue) Class (System.Collections.Generic)[^]
KeyValuePair(TKey, TValue) Structure (System.Collections.Generic)[^]
 
Share this answer
 
v3
Comments
CPallini 8-May-18 11:27am    
My 5.
Maciej Los 8-May-18 15:37pm    
Thank you, Carlo.
[no name] 9-May-18 1:01am    
Thanks
Maciej Los 9-May-18 1:56am    
You're very welcome.
Please, accept my answer as a solution - formally - to remove your question from unanswered list.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900