将列表(对象)转换为列表(字符串) [英] Convert List(of object) to List(of string)

查看:48
本文介绍了将列表(对象)转换为列表(字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 c# 或 vb.net 中将 List(of Object) 转换为 List(of String) 而不遍历所有项目?(幕后迭代很好——我只想要简洁的代码)

Is there a way to convert a List(of Object) to a List(of String) in c# or vb.net without iterating through all the items? (Behind the scenes iteration is fine – I just want concise code)

更新:最好的方法可能只是做一个新的选择

Update: The best way is probably just to do a new select

myList.Select(function(i) i.ToString()).ToList();

myList.Select(i => i.ToString()).ToList();

推荐答案

不通过迭代来构建新列表是不可能的.您可以将列表包装在实现 IList 的容器中.

Not possible without iterating to build a new list. You can wrap the list in a container that implements IList.

您可以使用 LINQ 从这样的对象列表中获取 IEnumerable 的惰性求值版本:

You can use LINQ to get a lazy evaluated version of IEnumerable<string> from an object list like this:

var stringList = myList.OfType<string>();

这篇关于将列表(对象)转换为列表(字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆