我该如何转换IEnumerable< object>列出< IFoo>每个对象都是IFoo? [英] How can I convert IEnumerable<object> to List<IFoo> where each object is an IFoo?

查看:141
本文介绍了我该如何转换IEnumerable< object>列出< IFoo>每个对象都是IFoo?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 IEnumerable< object> 转换为 List< IFoo> ,其中每个对象都是的IFoo

我有一个 IEnumerable< object> someCollection ,并且 someCollection 中的每个项目都是 IFoo 实例。如何将 someCollection 转换为 List< IFoo> ?我可以使用转换或转换或其他东西,而不是循环和建立一个列表吗?使用LINQ,你可以使用 使用LINQ, a href =http://msdn.microsoft.com/en-us/library/bb341406.aspx> Cast 来投射这些项目,并使用 ToList 获取列表。



尝试:

  IEnumerable< object> someCollection; //一些对象的枚举。 
var list = someCollection.Cast< IFoo>()。ToList();


How can I convert IEnumerable<object> to List<IFoo> where each object is an IFoo?

I have an IEnumerable<object>, someCollection, and each item in someCollection is an IFoo instance. How can I convert someCollection into a List<IFoo>? Can I use convert or cast or something instead of looping through and building up a list?

解决方案

Using LINQ, you can use Cast to cast the items, and use ToList to get a list.

Try:

 IEnumerable<object> someCollection; //Some enumerable of object.
 var list = someCollection.Cast<IFoo>().ToList();

这篇关于我该如何转换IEnumerable&lt; object&gt;列出&lt; IFoo&gt;每个对象都是IFoo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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