如何转换键值对类型列表(从对象到字符串)

发布于 2024-10-24 07:05:47 字数 41 浏览 1 评论 0原文

如何将键值对(字符串、对象)列表转换为键值对(字符串、字符串)列表?

How do I convert a list of key-value pairs (String, Object) to a list of key value pairs (string, string)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

茶色山野 2024-10-31 07:05:47

使用 LINQ,您可以执行如下操作:

var newList = oldList.Select(kv => new KeyValuePair<string, string>(kv.Key, kv.Value.ToString())).ToList()

当然,如果 ToString 表示是某种复杂类型,则它可能不完全是您所期望的,但您明白了。

Using LINQ, you can do something like this:

var newList = oldList.Select(kv => new KeyValuePair<string, string>(kv.Key, kv.Value.ToString())).ToList()

Of course, the ToString representation may not be exactly what you are expecting if it's some complex type, but you get the idea.

凉薄对峙 2024-10-31 07:05:47
List<KeyValuePair<string, object>> list = ...
List<KeyValuePair<string, object>> castedItemsList = 
 list
    .Select(item => new KeyValuePair<string, string>(item.Key, item.Value.ToString()))
    .ToList();
List<KeyValuePair<string, object>> list = ...
List<KeyValuePair<string, object>> castedItemsList = 
 list
    .Select(item => new KeyValuePair<string, string>(item.Key, item.Value.ToString()))
    .ToList();
旧人九事 2024-10-31 07:05:47

如果值是字符串类型,您可以直接转换它:

var result = source.Select(x => new KeyValuePair<string,string>(x.key,(string)x.value)

如果需要转换值,您可以按照建议使用 .ToString() 或使用转换器函数。也许您的第二个对象是具有 First 和 LastName 属性的 Person 类,并且您希望获得全名作为结果:

var result = source.Select(x => new KeyValuePair<string,string>(x.key, string.Format("{0} {1}", ((Person)x.Value).FirstName, ((Person)x.Value).SecondName))

当然您可以使用一种方法来简化转换

var result = source.Select(Convert);

private KeyValuePair<string,string> Convert(KeyValuePair<string,object> pair)
{
    var key = pair.Key;
    var person = (Person)pair.Value;
    var fullName = string.Format("{0} {1}", person.FirstName, person.LastName);

    return new KeyValuePair<string,string>(key, fullName);
}

If the value is of type string you can just cast it:

var result = source.Select(x => new KeyValuePair<string,string>(x.key,(string)x.value)

If the value needs to be converted you can either use .ToString() as already suggested or use a converter function. Maybe your second object is a Person class with a First and LastName Property and you want to get a full name as result:

var result = source.Select(x => new KeyValuePair<string,string>(x.key, string.Format("{0} {1}", ((Person)x.Value).FirstName, ((Person)x.Value).SecondName))

Of course you can use a method to simplify your conversion

var result = source.Select(Convert);

private KeyValuePair<string,string> Convert(KeyValuePair<string,object> pair)
{
    var key = pair.Key;
    var person = (Person)pair.Value;
    var fullName = string.Format("{0} {1}", person.FirstName, person.LastName);

    return new KeyValuePair<string,string>(key, fullName);
}
ヅ她的身影、若隐若现 2024-10-31 07:05:47

您必须投射这些对象。

You would have to Cast those objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文