Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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#_Linq_Dictionary_Lambda - Fatal编程技术网

C#:字典的转换<;字符串,字符串>;到字典<;字符串、字典<;字符串,字符串>&燃气轮机;

C#:字典的转换<;字符串,字符串>;到字典<;字符串、字典<;字符串,字符串>&燃气轮机;,c#,linq,dictionary,lambda,C#,Linq,Dictionary,Lambda,第一个词典如下 Dictionary<String, String> ParentDict = new Dictionary<String, String>(); ParentDict.Add("A_1", "1"); ParentDict.Add("A_2", "2"); ParentDict.Add("B_1", "3"); ParentDict.Add("B_2", "4");

第一个
词典
如下

        Dictionary<String, String> ParentDict = new Dictionary<String, String>();
        ParentDict.Add("A_1", "1");
        ParentDict.Add("A_2", "2");
        ParentDict.Add("B_1", "3");
        ParentDict.Add("B_2", "4"); 
        ParentDict.Add("C_1", "5");
现在我使用
nestedforloop
来实现这一点

如何使用
LNQ
LAMBDA表达式执行此操作?

请尝试:

var result = from p in ParentDict
             group p by p.Key[0] into g
             select new { Key = g.Key, Value = g };
这将为您提供一个{Key,Value}列表,其中Key将是“a”、“B”、“C”等,Value将是ParentDict中KeyValuePair的原始实例

您可以在此MSDN页面上找到更多LINQ示例查询:尝试:

var result = from p in ParentDict
             group p by p.Key[0] into g
             select new { Key = g.Key, Value = g };
这将为您提供一个{Key,Value}列表,其中Key将是“a”、“B”、“C”等,Value将是ParentDict中KeyValuePair的原始实例


您可以在此MSDN页面上找到更多LINQ示例查询:

我怀疑这样做的原因是因为您需要能够查找特定密钥字母的所有条目。在这种情况下,
查找
通常是更好的匹配:

var letterLookup = ParentDict.ToLookup(kv=>kv.Key[0]);
可以这样使用:

//letterLookup['A'] is an IEnumerable<KeyValuePair<string,string>>...

Console.WriteLine(string.Join(", ",
        letterLookup['A'].Select(kv=>kv.ToString()).ToArray()
    )); // [A_1, 1], [A_2, 2]

Console.WriteLine(new XElement("root",
        letterLookup['B'].Select(kv=>new XElement(kv.Key,kv.Value))
    ));// <root><B_1>3</B_1><B_2>4</B_2></root>


Console.WriteLine(letterLookup['B'].Any()); //true
Console.WriteLine(letterLookup['Z'].Any()); //false
//letterLookup['A']是一个IEnumerable。。。
Console.WriteLine(string.Join(“,”,
letterLookup['A']。选择(kv=>kv.ToString()).ToArray()
)); // [A_1,1],[A_2,2]
Console.WriteLine(新的XElement(“根”,
letterLookup['B']。选择(kv=>新元素(kv键,kv值))
));// 34
Console.WriteLine(letterLookup['B'].Any())//真的
Console.WriteLine(letterLookup['Z'].Any())//假的

与字典相比,查找的优势在于它可能包含任意给定键的多个值(与字典不同),并且如果某个键不存在,它具有一致的API:然后它返回空的枚举,而包含枚举的字典可能抛出KeyNotFoundException或返回null,或者返回空的可枚举项,这一切取决于您创建它的方式。

我怀疑这样做的原因是因为您需要能够查找特定密钥字母的所有条目。在这种情况下,
查找
通常是更好的匹配:

var letterLookup = ParentDict.ToLookup(kv=>kv.Key[0]);
 var result = ParentDict.GroupBy(p => p.Key[0].ToString())
                        .ToDictionary(g => g.Key, g => g.ToDictionary(x => x.Key, x => x.Value));
可以这样使用:

//letterLookup['A'] is an IEnumerable<KeyValuePair<string,string>>...

Console.WriteLine(string.Join(", ",
        letterLookup['A'].Select(kv=>kv.ToString()).ToArray()
    )); // [A_1, 1], [A_2, 2]

Console.WriteLine(new XElement("root",
        letterLookup['B'].Select(kv=>new XElement(kv.Key,kv.Value))
    ));// <root><B_1>3</B_1><B_2>4</B_2></root>


Console.WriteLine(letterLookup['B'].Any()); //true
Console.WriteLine(letterLookup['Z'].Any()); //false
//letterLookup['A']是一个IEnumerable。。。
Console.WriteLine(string.Join(“,”,
letterLookup['A']。选择(kv=>kv.ToString()).ToArray()
)); // [A_1,1],[A_2,2]
Console.WriteLine(新的XElement(“根”,
letterLookup['B']。选择(kv=>新元素(kv键,kv值))
));// 34
Console.WriteLine(letterLookup['B'].Any())//真的
Console.WriteLine(letterLookup['Z'].Any())//假的

与字典相比,查找的优势在于它可能包含任意给定键的多个值(与字典不同),并且如果某个键不存在,它具有一致的API:然后它返回空的枚举,而包含枚举的字典可能抛出KeyNotFoundException或返回null,或者返回空的可枚举项,这取决于您是如何创建的。

@Eamon Nerbonne:哦,对不起。。。这不是原因(我知道
查找
)。实际上我的需求是这样的…………最后我将把数据(以第二个字典的格式)转储到一个xml文件。。。。。(xml文件具有第二个字典的结构)添加了一个xml输出示例。@Eamon Nerbonne:噢,对不起。。。这不是原因(我知道
查找
)。实际上我的需求是这样的…………最后我将把数据(以第二个字典的格式)转储到一个xml文件。。。。。(xml文件具有第二个字典的结构)添加了一个具有XML输出的示例。为什么需要此数据结构?我询问的原因是嵌套字典通常比具有复合键或查找的单深度字典更慢、更不合适。为什么需要此数据结构?我询问的原因是嵌套字典通常比单深度字典更慢、更不合适具有复合键或查找功能的单深度词典。
 var result = ParentDict.GroupBy(p => p.Key[0].ToString())
                        .ToDictionary(g => g.Key, g => g.ToDictionary(x => x.Key, x => x.Value));