将 CollectionBase 转换为 List 或可用于 Linq 的数据类型
我正在使用 Aspose 单元格来操作 Excel 电子表格。 API 中的类型之一是电子表格中的图片集合,它派生自 CollectionBase:
我想将此类型转换为允许我使用 Linq 表达式的
类型对此的选择?
我想我可以迭代它并手动将其添加到新列表<图片> 但有更好的方法吗?
我读过这个问题 添加 IEnumerable
但我显然无法控制实现 CollectionBace 的类,因为它是第三方产品
I am using Aspose cells to manipulate Excel spreadsheets.
One of the types in the API is a collection of Pictures in the spreadsheet, which derives from CollectionBase:
see this link:
http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/aspose.cells.pictures.html
I want to convert this type to something that allows me to use Linq expressions
What are the options for this?
I guess I could iterate over it and manually add it to a new List<Picture>
But is there a better way to do this?
I have read this question
Adding IEnumerable<T> to class derived from CollectionBase
But I obviously don't have control over the class that implements CollectionBace as it is a third party product
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用
Enumerable.Cast()
< /a> 非通用IEnumerable
接口上的扩展方法,您可以在查询表达式中隐式执行:或显式执行,例如,如果您想使用点表示法:
Cast< 的替代方法;T>()
是OfType()
- 基本上忽略任何类型不正确的元素。在这种情况下,我认为Cast()
更合适。如果您出于某种原因想要将整个集合转换为
List
,这也很简单:Just use the
Enumerable.Cast<T>()
extension method on the non-genericIEnumerable
interface, which you can do implicitly in a query expression:or explicitly, for instance if you want to use dot notation:
An alternative to
Cast<T>()
isOfType<T>()
- which basically ignores any elements which aren't of the right type. In this case I thinkCast<T>()
is more appropriate though.If you want to convert the whole collection to a
List<T>
for whatever reason, that's easy too: