将列表<KeyValuePair<string, string>> 转换为列表<KeyValuePair<string, object>>
Convert List<KeyValuePair<string, string>> to List<KeyValuePair<string, object>>
将 List<KeyValuePair<string, string>>
转换为 List<KeyValuePair<string, object>>
的最佳方法是什么?
准确地说,我有 Dictionary<string, string>
,当我使用 ToList()
函数时,它会转换为 List<KeyValuePair<string, string>>
,但我想要 List<KeyValuePair<string, object>>
。如何做到这一点?此外,我希望它就位,而不是使用循环或任何其他不必要的代码行。谢谢
真实场景:
有一个 Web API 项目,在请求的端点之一 Body
中是 JsonElement
类型。要解析请求的 Body
,我这样做:
public JsonResult GetSomething([FromBody] JsonElement query){
string rawJson = query.ToString();
var keyValues = ParseRequestBodyHelper<Dictionary<string, string>>.ParseRequestBody(rawJson, "KeyValues");
}
ParseRequestBodyHelper
获取类型 (Dictionary<string, string>
) 以反序列化关于文件名 KeyValues
的对象。现在 keyValues
是 Dictionary<string, string>
.
反序列化后我想记录收到的信息。我的 Logger
得到这样的 KeyValuePairs 列表:
Logger.Instance.GetLogger().Information("Body of request parsed");
GetLogger
函数可以将 List<KeyValuePair<string, object>>
作为输入。我想记录 keyValues
以及其他信息,如下所示:
Logger.Instance.GetLogger(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("Ip", IP),
new KeyValuePair<string, object>("Methodname", "GetSomething"),
})
.Information("Body of request parsed");
如果我这样做
Logger.Instance.GetLogger(new List<KeyValuePair<string, object>>(keyValues.ToList())
{
new KeyValuePair<string, object>("Ip", IP),
new KeyValuePair<string, object>("Methodname", "GetSomething"),
})
.Information("Body of request parsed");
它给我一个错误,因为 keyValues.ToList()
类型是 List<KeyValuePair<string, string>>
但我想要 List<KeyValuePair<string, object>>
.
所以现在我的整个问题都在这里。您认为如何解决这个问题?
使用 Select
创建所需类型的 KeyValuePair
:
Dictionary<string,string> dictionary = new Dictionary<string,string>();
// fill with values
List<KeyValuePair<string,object>> objList = dictionary.Select(x => new KeyValuePair<string,object>(x.Key, x.Value)).ToList();
将 List<KeyValuePair<string, string>>
转换为 List<KeyValuePair<string, object>>
的最佳方法是什么?
准确地说,我有 Dictionary<string, string>
,当我使用 ToList()
函数时,它会转换为 List<KeyValuePair<string, string>>
,但我想要 List<KeyValuePair<string, object>>
。如何做到这一点?此外,我希望它就位,而不是使用循环或任何其他不必要的代码行。谢谢
真实场景:
有一个 Web API 项目,在请求的端点之一 Body
中是 JsonElement
类型。要解析请求的 Body
,我这样做:
public JsonResult GetSomething([FromBody] JsonElement query){
string rawJson = query.ToString();
var keyValues = ParseRequestBodyHelper<Dictionary<string, string>>.ParseRequestBody(rawJson, "KeyValues");
}
ParseRequestBodyHelper
获取类型 (Dictionary<string, string>
) 以反序列化关于文件名 KeyValues
的对象。现在 keyValues
是 Dictionary<string, string>
.
反序列化后我想记录收到的信息。我的 Logger
得到这样的 KeyValuePairs 列表:
Logger.Instance.GetLogger().Information("Body of request parsed");
GetLogger
函数可以将 List<KeyValuePair<string, object>>
作为输入。我想记录 keyValues
以及其他信息,如下所示:
Logger.Instance.GetLogger(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("Ip", IP),
new KeyValuePair<string, object>("Methodname", "GetSomething"),
})
.Information("Body of request parsed");
如果我这样做
Logger.Instance.GetLogger(new List<KeyValuePair<string, object>>(keyValues.ToList())
{
new KeyValuePair<string, object>("Ip", IP),
new KeyValuePair<string, object>("Methodname", "GetSomething"),
})
.Information("Body of request parsed");
它给我一个错误,因为 keyValues.ToList()
类型是 List<KeyValuePair<string, string>>
但我想要 List<KeyValuePair<string, object>>
.
所以现在我的整个问题都在这里。您认为如何解决这个问题?
使用 Select
创建所需类型的 KeyValuePair
:
Dictionary<string,string> dictionary = new Dictionary<string,string>();
// fill with values
List<KeyValuePair<string,object>> objList = dictionary.Select(x => new KeyValuePair<string,object>(x.Key, x.Value)).ToList();