Dictionary to List> where the Value in KeyValuePair is MyClass.ToString()

前端 未结 2 1397
轮回少年
轮回少年 2021-01-29 07:14
Dictionary dict = new Dictionary();

//where MyClass has an override for ToString()

Now how do I get a

相关标签:
2条回答
  • 2021-01-29 07:33

    Use LINQ:

    var list = dict.Select(k => 
      new KeyValuePair<string,string>(k.Key, k.Value.ToString()))
      .ToList();
    
    0 讨论(0)
  • 2021-01-29 07:37

    Not tested/compiled, but something like that should work:

    dict.Select(kvp => new KeyValuePair<string, string>(kvp.Key, kvp.Value.ToString())).ToList()
    

    if the syntax is not 100% spot on, I hope you got the idea.

    0 讨论(0)
提交回复
热议问题