Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 转换列表<;字典<;字符串,字符串>&燃气轮机;放入一本词典<;字符串,字符串>;_C#_.net_Dictionary - Fatal编程技术网

C# 转换列表<;字典<;字符串,字符串>&燃气轮机;放入一本词典<;字符串,字符串>;

C# 转换列表<;字典<;字符串,字符串>&燃气轮机;放入一本词典<;字符串,字符串>;,c#,.net,dictionary,C#,.net,Dictionary,我有一个C语言词典列表,如下所示: List<Dictionary<string, string>> dict 以及: 现在,我希望生成的词典包含: Dictionary<string, string> reultDict = { "Name": "test1", "ID" : "100" } Dictionary reultDict={“Name”:“test1”,“ID”

我有一个C语言词典列表,如下所示:

List<Dictionary<string, string>> dict 
以及:

现在,我希望生成的
词典
包含:

Dictionary<string, string> reultDict = { "Name": "test1", "ID" : "100" }
Dictionary reultDict={“Name”:“test1”,“ID”:“100”}
您可以使用LINQ:

var result = dict.SelectMany(c => c).ToDictionary(c => c.Key, c => c.Value);
在此处测试工作代码:

List<Dictionary<string, string>> dict = new List<Dictionary<string, string>>()
{ 
    new Dictionary<string, string>(), new Dictionary<string, string>()
};

dict[0].Add("Name", "test1");
dict[1].Add("ID", "100");

var result = dict.SelectMany(c => c).ToDictionary(c => c.Key, c => c.Value);

试试这个
var result=dict.ToDictionary(d=>d.Values.First())这不起作用。我恐怕
dict[0]={“key”:“Name”,“value”:“test1”}
不是有效的C代码。它抛出一个无效的语法错误。你能把实际的代码发出来吗?这两本字典能包含一个键相同的项目吗?如果是这样,该键的值在两个字典中是否会不同?我的意思是,dict[0]将有两个键值对,如{“key”:“Name”}和{“value”:“test1”}。类似地,dict[1]将有{“key”:“ID”}和{“value”:“100”}它就在那里。与此相反:
dict.SelectMany(c=>c)
,您需要类似于
dict.Select(c=>newkeyvaluepair(c[“key”]、c[“value”])
。或者只需要
dict.ToDictionary(d=>d[“key”]、d=>d[“value”])
如果有可能存在共享密钥的词典,这将不起作用。请使用
dict[0]尝试您的代码。添加(“名称”,“test1”)
dict[1]。添加(“Name”,“100”);
,由于重复的
Name
键,它将抛出。
var result = dict.SelectMany(c => c).ToDictionary(c => c.Key, c => c.Value);
List<Dictionary<string, string>> dict = new List<Dictionary<string, string>>()
{ 
    new Dictionary<string, string>(), new Dictionary<string, string>()
};

dict[0].Add("Name", "test1");
dict[1].Add("ID", "100");

var result = dict.SelectMany(c => c).ToDictionary(c => c.Key, c => c.Value);
dict.Select(c => new KeyValuePair<string, string>(c["key"], c["value"])
dict.ToDictionary(d => d["key"], d => d["value"])