对仅具有GetEnumerator()的对象使用LINQ [英] Using LINQ for an object that only has GetEnumerator()

查看:82
本文介绍了对仅具有GetEnumerator()的对象使用LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以在仅公开System.Collections.IEnumerator中的Add(),Remove(),Count(),Item()和GetEnumerator()的对象中使用LINQ吗?

Can you use LINQ in an object that exposes only Add(), Remove(), Count(), Item() and GetEnumerator() from System.Collections.IEnumerator?

推荐答案

现有的Linq扩展方法可用于实现IEnumerable<T>的对象.我假设您的对象实现了非通用的IEnumerable接口.在这种情况下,您可以使用Cast<T>扩展方法来获取通用的IEnumerable<T>包装器.例如,如果元素的类型为int:

The existing Linq extension methods work on objects that implement IEnumerable<T>. I assume your object implements the non-generic IEnumerable interface. In that case you can use the Cast<T> extension method to get a generic IEnumerable<T> wrapper. For instance, if the elements are of type int :

var wrapper = myObject.Cast<int>();

您现在可以在包装器上使用Linq

You can now use Linq on the wrapper

这篇关于对仅具有GetEnumerator()的对象使用LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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