[英]How to collect values for all the keys from a Dictionary<string,object> in a List
I have a Dictionary<string, Data>
collection. 我有一个
Dictionary<string, Data>
集合。
My Data class is has multiple members. 我的数据类具有多个成员。
public class Data
{
public string docid{get;set;}
public string filename{get; set;}
public string doctype{get;set;}
}
so, each key in the dictionary has a list of values. 因此,字典中的每个键都有一个值列表。 I want to write the values for all the keys in a csv file.
我想将所有键的值写入一个csv文件中。
My csv file will have columns of 我的csv文件将包含以下列
I have been using LINQ2CSV library for writing into csv, but since it accepts a list and a filepath as a parameter, i am not able to collect values for all the keys into a list. 我一直在使用LINQ2CSV库写入csv,但是由于它接受列表和文件路径作为参数,因此我无法将所有键的值收集到列表中。 please suggest a better way to achieve this .
请提出一个更好的方法来实现这一目标。
LINQ2CSV's Write
method accepts IEnumerable<T>
. LINQ2CSV的
Write
方法接受IEnumerable<T>
。 Your Values
in dictionary are available as an array. 您在字典中的
Values
可以作为数组使用。 You can do: 你可以做:
cc.Write(yourDictionary.Values, "filePath.csv");
There is no need for getting List<Data>
from Dictionary Values
, but if you still want to create a List<Data>
you can do: 不需要从Dictionary
Values
获取List<Data>
,但是如果您仍想创建List<Data>
,则可以执行以下操作:
List<Data> myList = yourDictionary.Values.ToList();
or 要么
List<Data> myList = new List<Data>(yourDicitionary.Values);
您可以将字典的值复制到列表中:
var list = dictionary.Values.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.