本文介绍了Cast嵌套列表< X>到嵌套列表< Y>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我知道可以将一个项目的列表从一种类型转换为另一种类型,但是如何将嵌套列表转换为嵌套列表。 已尝试的解决方案: / p> 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]'键入'System.Collections .Generic.List 1 [System.String]'。 解决方案您可以使用 Select()而不是这样: List< List< String>> new_list = abc.Select(x => x.Select(y => y.ToString())。ToList())。ToList(); 此异常的原因: code> Cast 会抛出 InvalidCastException ,因为它尝试转换 List< int> 到 object ,然后将其转换为 List< string> : List< int> myListInt = new List< int> {5,4}; object myObject = myListInt; List< string> myListString =(List< string>)myObject; //此处抛出异常 因此,这是不可能的。即使,您也不能将 int 转换为 string 。 int myInt = 11; object myObject = myInt; string myString =(string)myObject; //这里抛出异常 这个异常的原因是 方块值 只能取消装箱为 完全相同类型的变量 。 p>以下是 的实现, 如果您感兴趣的话,可以使用< TResult>(this IEnumerable source) public static IEnumerable< TResult> Cast< TResult>(此IEnumerable源){ IEnumerable< TResult> typedSource = source as IEnumerable< TResult> ;; if(typedSource!= null)return typedSource; if(source == null)throw Error.ArgumentNull(source); return CastIterator< TResult>(source); } 如您所见,它返回 CastIterator : static IEnumerable< TResult> CastIterator< TResult>(IEnumerable source){ foreach(object obj in source)yield return(TResult)obj; } 看看上面的代码。它将使用 foreach 循环遍历源代码,并将所有项目转换为 object ,然后转换为 (TResult)。 I know its possible to cast a list of items from one type to another but how do you cast a nested list to nested List .Already tried solutions:List<List<String>> new_list = new List<List<string>>(abc.Cast<List<String>>());andList<List<String>> new_list = abc.Cast<List<String>>().ToList();Both of which give the following error: Unable to cast object of type 'System.Collections.Generic.List1[System.Int32]' to type 'System.Collections.Generic.List1[System.String]'. 解决方案 You can use Select() instead of that way:List<List<String>> new_list = abc.Select(x => x.Select(y=> y.ToString()).ToList()).ToList();The reason of this exception: Cast will throw InvalidCastException, because it tries to convert List<int> to object, then cast it to List<string>:List<int> myListInt = new List<int> { 5,4};object myObject = myListInt;List<string> myListString = (List<string>)myObject; // Exception will be thrown hereSo, this is not possible. Even, you can't cast int to string also.int myInt = 11;object myObject = myInt;string myString = (string)myObject; // Exception will be thrown hereThe reason of this exception is, a boxed value can only be unboxed to a variable of the exact same type.Additional Information:Here is the implemetation of the Cast<TResult>(this IEnumerable source) method, if you interested:public static IEnumerable<TResult> Cast<TResult>(this IEnumerable source) { IEnumerable<TResult> typedSource = source as IEnumerable<TResult>; if (typedSource != null) return typedSource; if (source == null) throw Error.ArgumentNull("source"); return CastIterator<TResult>(source);}As you see, it returns CastIterator:static IEnumerable<TResult> CastIterator<TResult>(IEnumerable source) { foreach (object obj in source) yield return (TResult)obj;}Look at the above code. It will iterate over source with foreach loop, and converts all items to object, then to (TResult). 这篇关于Cast嵌套列表< X>到嵌套列表< Y>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 12:42