【发布时间】:2015-02-21 05:55:22
【问题描述】:
我知道可以将项目列表从一种类型转换为另一种类型,但是如何将嵌套列表转换为嵌套列表。
已经尝试过的解决方案:
List<List<String>> new_list = new List<List<string>>(abc.Cast<List<String>>());
和
List<List<String>> new_list = abc.Cast<List<String>>().ToList();
两者都给出以下错误:
无法转换类型的对象 'System.Collections.Generic.List
1[System.Int32]' to type 'System.Collections.Generic.List1[System.String]'。
【问题讨论】:
-
无论如何,您都不能将
int转换为string,因此即使您没有嵌套列表,它仍然无法正常工作。 -
如果你的目的只是要改变泛型元素的类型,你可以创建一个泛型扩展方法,返回值为 T.ToString()
标签: c# list casting nested nested-lists