本文介绍了如何在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;
                      }
            }



从上面如何使用列表添加dicationary值



这里下面这行字典值是


from the above how to add dicationary values using list

here in this below line dictionary value is there

foreach (var data in dict)



如何添加字典值列表在c#



从我上面如何将字典值添加到c列表中#



我尝试过:




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:

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;
                      }
            }



从上面如何使用列表添加dicationary值



这里下面这行字典值是


from the above how to add dicationary values using list

here in this below line dictionary value is there

foreach (var data in dict)



如何添加字典值列表在c#



从我上面如何将字典值添加到c#


how to add dictionary value to list in c#

from my above how to add dictionary value to list in 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:

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)[^]


这篇关于如何在C#中将字典值添加到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 14:04