为什么 Controls 集合不提供所有 IEnumerable 方法?

发布于 2024-09-11 00:26:42 字数 469 浏览 7 评论 0原文

我不确定 ASP.Net 的 ControlCollection 是如何工作的,所以也许有人可以为我阐明这一点。

我最近发现了扩展方法和 Linq 的神奇之处。好吧,我很遗憾地发现这不是有效的语法

var c=Controls.Where(x => x.ID=="Some ID").SingleOrDefault();

但是据我所知, Controls 确实实现了提供此类方法的 IEnumerable 接口,那么什么给了?为什么这不起作用?我至少为这个问题找到了一个不错的解决方法:

var list = (IEnumerable<Control>)Controls;
var this_item = list.Where(x => x.ID == "Some ID").SingleOrDefault();

I'm not for sure how the ControlCollection of ASP.Net works, so maybe someone can shed some light on this for me.

I recently discovered the magic that is extension methods and Linq. Well, I was very sad to find that this isn't valid syntax

var c=Controls.Where(x => x.ID=="Some ID").SingleOrDefault();

However from what I can tell, Controls does implement the IEnumerable interface which provides such methods, so what gives? Why doesn't that just work? I have found a decent work around for this issue at least:

var list = (IEnumerable<Control>)Controls;
var this_item = list.Where(x => x.ID == "Some ID").SingleOrDefault();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

智商已欠费 2024-09-18 00:26:42

不,IEnumerable 上没有很多扩展方法:IEnumerable 有。尽管 IEnumerable 扩展了 IEnumerable,但它们是两个独立的接口。

正常的 LINQ 转换方法是使用 Cast()OfType()< /code>扩展非泛型接口的扩展方法:

IEnumerable<TextBox> textBoxes = Controls.OfType<TextBox>();
IEnumerable<Control> controls = Controls.Cast<Control>();

两者之间的区别在于 OfType 将跳过任何不属于必需项的项目类型; Cast 将抛出异常。

一旦获得了对通用 IEnumerable 类型的引用,所有其余的 LINQ 方法都可用。

No, IEnumerable doesn't have many extension methods on it: IEnumerable<T> does. They are two separate interfaces, although IEnumerable<T> extends IEnumerable.

The normal LINQ ways of converting are to use the Cast<T>() and OfType<T>() extension methods which do extend the nongeneric interface:

IEnumerable<TextBox> textBoxes = Controls.OfType<TextBox>();
IEnumerable<Control> controls = Controls.Cast<Control>();

The difference between the two is that OfType will just skip any items which aren't of the required type; Cast will throw an exception instead.

Once you've got references to the generic IEnumerable<T> type, all the rest of the LINQ methods are available.

三寸金莲 2024-09-18 00:26:42

这只是因为 ControlCollection 类在泛型之前出现;因此它实现了 IEnumerable,但没有实现 IEnumerable

幸运的是,IEnumerable 接口上确实存在一个 LINQ 扩展方法,允许您通过转换生成一个 IEnumerableCast。这意味着您始终可以这样做:

var c = Controls.Cast<Control>().Where(x => x.ID == "Some ID").SingleOrDefault();

This is just because the ControlCollection class came around before generics; so it implements IEnumerable but not IEnumerable<Control>.

Fortunately, there does exist a LINQ extension method on the IEnumerable interface that allows you to generate an IEnumerable<T> through casting: Cast<T>. Which means you can always just do this:

var c = Controls.Cast<Control>().Where(x => x.ID == "Some ID").SingleOrDefault();
榆西 2024-09-18 00:26:42

除了 Jon Skeet 和 Dan Tao 提供的答案之外,您还可以通过显式提供类型来使用查询表达式语法。

Control myControl = (from Control control in this.Controls
                    where control.ID == "Some ID"
                    select control).SingleOrDefault();

In addition to the answers provided by Jon Skeet and Dan Tao, you can use query expression syntax by explicitly providing the type.

Control myControl = (from Control control in this.Controls
                    where control.ID == "Some ID"
                    select control).SingleOrDefault();
雨后咖啡店 2024-09-18 00:26:42

Linq 使用通用集合。 ControlsCollection 实现 IEnumerable 而不是 IEnumberable

如果您发现这不起作用

((IEnumerable)page.Controls).Where(...

但是,这确实有效

((IEnumerable<Control>)page.Controls).Where(...

您可以转换为通用 IEnumerable或者访问一个扩展方法,如下所示:

 page.Controls.OfType<Control>().Where(c => c.ID == "Some ID").FirstOrDefault();

Linq utilized Generic Collections. ControlsCollection implements IEnumerable not IEnumberable<T>

If you notice this will not work

((IEnumerable)page.Controls).Where(...

However, this does

((IEnumerable<Control>)page.Controls).Where(...

You can either cast to Generic IEnumerable<T> or access an extension method that does, like so:

 page.Controls.OfType<Control>().Where(c => c.ID == "Some ID").FirstOrDefault();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文