Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 如何将包含逗号分隔字符串值的字典转换为KeyValuePairs集合?_C#_Linq - Fatal编程技术网

C# 如何将包含逗号分隔字符串值的字典转换为KeyValuePairs集合?

C# 如何将包含逗号分隔字符串值的字典转换为KeyValuePairs集合?,c#,linq,C#,Linq,我有一本字典,像: Dictionary<string, string> myDict = new Dictionary<string, string> { { "key1", "value1,value2,value3" }, { "key2", "value4,value5" } } 抱歉,问题主题可能不清楚。此转换的关键是SelectMany方法: 此转换的关键是SelectMany方法: 不使用linq

我有一本字典,像:

    Dictionary<string, string> myDict = new Dictionary<string, string>
    {
        { "key1", "value1,value2,value3" },
        { "key2", "value4,value5" }
    }

抱歉,问题主题可能不清楚。

此转换的关键是SelectMany方法:


此转换的关键是SelectMany方法:

不使用linq

  public List<KeyValuePair<string, string>> GenerateKeyValuePair(Dictionary<string,string> dict) {
        List<KeyValuePair<string, string>> List = new List<KeyValuePair<string, string>>();
        foreach (var item in dict)
        {
                string[] values = item.Value.Split(',');
                for (int i = 0; i < values.Length; i++)
                {
                    List.Add(new KeyValuePair<string, string>(item.Key, values[i].ToString()));
                }
        }
        return List;
    }
希望有帮助,顺便说一句,Linq是不使用Linq的最短答案

  public List<KeyValuePair<string, string>> GenerateKeyValuePair(Dictionary<string,string> dict) {
        List<KeyValuePair<string, string>> List = new List<KeyValuePair<string, string>>();
        foreach (var item in dict)
        {
                string[] values = item.Value.Split(',');
                for (int i = 0; i < values.Length; i++)
                {
                    List.Add(new KeyValuePair<string, string>(item.Key, values[i].ToString()));
                }
        }
        return List;
    }

希望有帮助,顺便说一句,Linq是最短的答案

它不会编译它不会编译:List myList=myDict.Keys.Selectx=>myDict[x]。Splitnew char[]{',}。Selecty=>new KeyValuePairx,y.SelectManyy=>y.ToList;试试这个:List myList=myDict.Keys.Selectx=>myDict[x].splitnewchar[]{',}.Selecty=>newkeyvaluepairx,y.SelectManyy=>y.ToList;if item.Value.Contains“”无效。您可以完全删除if,您的代码也可以正常工作。if item.Value.Contains“”是无用的。您可以完全删除if,您的代码也可以正常工作
  public List<KeyValuePair<string, string>> GenerateKeyValuePair(Dictionary<string,string> dict) {
        List<KeyValuePair<string, string>> List = new List<KeyValuePair<string, string>>();
        foreach (var item in dict)
        {
                string[] values = item.Value.Split(',');
                for (int i = 0; i < values.Length; i++)
                {
                    List.Add(new KeyValuePair<string, string>(item.Key, values[i].ToString()));
                }
        }
        return List;
    }