Cast嵌套列表< X>到嵌套列表< Y> [英] Cast Nested List<X> to Nested List<Y>
问题描述
我知道可以将一个项目的列表从一种类型转换为另一种类型,但是如何将嵌套列表转换为嵌套列表。
已尝试的解决方案: / p>
List< List< String>> new_list = new List< List< string>>(abc.Cast< List< String>>());
和
List< List< String>> new_list = abc.Cast< List< String>>()。ToList();
两者都会出现以下错误:
< blockquote>
无法转换类型
的对象System.Collections.Generic.List 1 [System.Int32]'键入
1 [System.String]'。
'System.Collections .Generic.List
您可以使用 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>>());
and
List<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.List
1[System.Int32]' to type 'System.Collections.Generic.List
1[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 here
So, 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 here
The 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>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!