Why doesn't the Controls collection provide all of the IEnumerable methods?

前端 未结 4 1576
暖寄归人
暖寄归人 2020-12-29 18:33

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

4条回答
  •  自闭症患者
    2020-12-29 19:02

    Linq utilized Generic Collections. ControlsCollection implements IEnumerable not IEnumberable

    If you notice this will not work

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

    However, this does

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

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

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

提交回复
热议问题