创建了IEnumerable< KeyValuePair<字符串,字符串>>用C#对象?字符串、对象、LT、IEnumerable

2023-09-04 01:39:52 作者:怕孤厌闹

出于测试目的,我需要创建一个的IEnumerable< KeyValuePair<字符串,字符串>>在下面的示例键值对目标:

For testing purposes, I need to create an IEnumerable<KeyValuePair<string, string>> object with the following sample key value pairs:

Key = Name | Value : John
Key = City | Value : NY

什么是做到这一点的最简单的方法呢?

What is the easiest approach to do this?

推荐答案

任何的:

values = new Dictionary<string,string> { {"Name", "John"}, {"City", "NY"} };

values = new [] {
      new KeyValuePair<string,string>("Name","John"),
      new KeyValuePair<string,string>("City","NY")
    };

values = (new[] {
      new {Key = "Name", Value = "John"},
      new {Key = "City", Value = "NY"}
   }).ToDictionary(x => x.Key, x => x.Value);