演员表&LT; X&GT;列出&LT; Y&GT; [英] Cast List<X> to List<Y>
本文介绍了演员表&LT; X&GT;列出&LT; Y&GT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道它可能从一种类型的投项目的列表到另一个(假设你的对象有一个公共静态明确经营者的方法做铸件)一次如下:
列表&LT; Y&GT; ListOfY =新的List&LT; Y&GT;();的foreach(在ListOfX X X)
ListOfY.Add((Y)X);
但它是不可能在同一时间内施放整个列表?例如,
ListOfY =(列表&LT; Y&GT;)ListOfX;
解决方案
如果 X
真的可以转换为是
你应该能够使用
列表&LT; Y&GT; listOfY = listOfX.Cast&LT; Y方式&gt;()了ToList()
I know its possible to cast a list of items from one type to another (given that your object has a public static explicit operator method to do the casting) one at a time as follows:
List<Y> ListOfY = new List<Y>();
foreach(X x in ListOfX)
ListOfY.Add((Y)x);
But is it not possible to cast the entire list at one time? For example,
ListOfY = (List<Y>)ListOfX;
解决方案
If X
can really be cast to Y
you should be able to use
List<Y> listOfY = listOfX.Cast<Y>().ToList()
这篇关于演员表&LT; X&GT;列出&LT; Y&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文