本文翻译自:IEnumerable and Recursion using yield return

I have an IEnumerable<T> method that I'm using to find controls in a WebForms page. 我有一个IEnumerable<T>方法,用于在WebForms页面中查找控件。

The method is recursive and I'm having some problems returning the type I want when the yield return is returnig the value of the recursive call. 该方法是递归的,当yield return为returnig递归调用的值时, yield return想要的类型时遇到一些问题。

My code looks as follows: 我的代码如下所示:

    public static IEnumerable<Control> GetDeepControlsByType<T>(this Control control){foreach(Control c in control.Controls){if (c is T){yield return c;}if(c.Controls.Count > 0){yield return c.GetDeepControlsByType<T>();}}}

This currently throws a "Cannot convert expression type" error. 当前,这将引发“无法转换表达式类型”错误。 If however this method returns type IEnumerable<Object> , the code builds, but the wrong type is returned in the output. 但是,如果此方法返回类型IEnumerable<Object> ,则将构建代码,但是在输出中返回了错误的类型。

Is there a way of using yield return whilst also using recursion? 有没有办法在使用yield return同时也使用递归?


#1楼

参考:https://stackoom.com/question/8cq7/IEnumerable和使用收益回报的递归


#2楼

Others provided you with the correct answer, but I don't think your case benefits from yielding. 其他人为您提供了正确的答案,但我认为您的案子不会因屈服而受益。

Here's a snippet which achieves the same without yielding. 这是一个无需屈服即可实现相同功能的代码段。

public static IEnumerable<Control> GetDeepControlsByType<T>(this Control control)
{return control.Controls.Where(c => c is T).Concat(control.Controls.SelectMany(c =>c.GetDeepControlsByType<T>()));
}

#3楼

Inside a method that returns IEnumerable<T> , yield return has to return T , not an IEnumerable<T> . 在返回IEnumerable<T>的方法内部, yield return必须返回T ,而不是IEnumerable<T>

Replace 更换

yield return c.GetDeepControlsByType<T>();

with: 与:

foreach (var x in c.GetDeepControlsByType<T>())
{yield return x;
}

#4楼

You need to yield each of the items yielded by the recursive call: 您需要产生递归调用产生的每个项目

public static IEnumerable<Control> GetDeepControlsByType<T>(this Control control)
{foreach(Control c in control.Controls){if (c is T){yield return c;}if(c.Controls.Count > 0){foreach (Control control in c.GetDeepControlsByType<T>()){yield return control;}}}
}

Note that there's a cost to recursing in this way - you'll end up creating a lot of iterators, which can create a performance issue if you have a really deep control tree. 请注意,以这种方式进行递归会产生成本-您最终将创建大量的迭代器,如果您的控制树非常深,则可能会导致性能问题。 If you want to avoid that, you basically need to do the recursion yourself within the method, to make sure there's only one iterator (state machine) created. 如果要避免这种情况,则基本上需要在方法中自己进行递归,以确保仅创建一个迭代器(状态机)。 See this question for more details and a sample implementation - but this obviously adds a certain amount of complexity too. 有关更多详细信息和示例实现,请参见此问题 -但这显然也增加了一定数量的复杂性。


#5楼

You need to return the items from the enumerator, not the enumerator itself, in your second yield return 你需要从枚举,而不是枚举本身,返回你的第二个项目 yield return

public static IEnumerable<Control> GetDeepControlsByType<T>(this Control control)
{foreach (Control c in control.Controls){if (c is T){yield return c;}if (c.Controls.Count > 0){foreach (Control ctrl in c.GetDeepControlsByType<T>()){yield return ctrl;}}}
}

#6楼

I think you have to yield return each of the controls in the enumerables. 我认为您必须让值返回枚举中的每个控件。

    public static IEnumerable<Control> GetDeepControlsByType<T>(this Control control){foreach (Control c in control.Controls){if (c is T){yield return c;}if (c.Controls.Count > 0){foreach (Control childControl in c.GetDeepControlsByType<T>()){yield return childControl;}}}}

IEnumerable和使用收益回报的递归相关推荐

  1. 熊市赚取被动收入:质押NFT带来高达30%收益回报!

    9月15日,以太坊宣布合并成功,意味着eth质押挖矿正式开始!加密市场资金纷纷投入eth质押平台以赚取收益率,在全球通货膨胀和美联储加息的双重背景下,人们都在思考如何提高资金利用率? 而大多数人所不知 ...

  2. 网格交易策略(附策略源码与收益图)

    网格交易策略简介 什么是网格交易策略? 网格交易是利用市场震荡行情获利的一种主动交易策略,其本质是利用投资标的在一段震荡行情中价格在网格区间内的反复运动以进行加仓减仓的操作以达到投资收益最大化的目的. ...

  3. 中秋节盘点一下自己的收益!

    今天上证指数已经是从高位调整到第四天,基本长处于前期的强烈支撑位3600点附近,盘中的两次低位探底也说明了一些情况,今天成交量些许缩量,但两市依旧维持在13000亿以上,而且创出了自2015年7月份以 ...

  4. DeFi收益来源全面概述

    去中心化金融一个主要的优势就是它对所有人开放,任何人在任何时间.任何地点都可以参与其中.这样一来,作为DeFi参与者就有机会获得在传统金融领域很难获得或根本不可能获得的收益. 加密货币的特性是开源的. ...

  5. C#中使用的yield关键字是什么?

    在" 如何仅显示IList <>的片段"问题中,答案之一具有以下代码片段: IEnumerable<object> FilteredList() {fore ...

  6. MachineLearning(6)-Daviad Silver强化学习课程脉络整理

    强化学习-Daviad Silver强化学习课程脉络整理 1.lecture1 introduction 1.1 强化学习简介 1.2 强化学习类别 1.3 强化学习的主要问题 2.lecture2 ...

  7. python基于粒子群优化的投资组合优化

    我今年的研究课题是使用粒子群优化(PSO)的货币进位交易组合优化.在本文中,我将介绍投资组合优化并解释其重要性.其次,我将演示粒子群优化如何应用于投资组合优化.第三,我将解释套利交易组合,然后总结我的 ...

  8. 程序员又背锅?美团外卖声明“杀熟会员”是技术原因,软件定位缓存导致配送费不准!网友:程序员太惨!...

    前几天的"美团杀熟外卖会员"事件你听说了吗? 简单地说,有人爆料自己开通美团会员后,以前常点的一家外卖店配送费由平时的2元变为6元.不仅是一家店这种情况,一部开通美团外卖会员的手机 ...

  9. 六月第一枪:股市震荡,下一步该做什么?

    相信大家都看到了,现在的A股震荡,自美国将中国进口产品的关税从10%提高到25%开始.市场在第一周大跌后进入横盘阶段,5月第一周上证综指从4月底的3078点大幅跳空跌至2845点,之后三周在2900点 ...

最新文章

  1. 用for语句设置密码
  2. oracle 存储过程 ,触发器练习
  3. JZOJ 4676. 【NOIP2016提高A组模拟7.21】模板串
  4. 生成对抗网络(GAN)的统计推断
  5. Oracle 数据类型及存储方式(袁光东 原创)
  6. 你不知道的 Chrome DevTools 玩法
  7. WCF开发入门的六个步骤
  8. 力扣69-x的平方根(解决一个问题:我的答案和题解很像,但是为什么过不了?C++、Java版)
  9. 为什么i3的cpu基础频率最高,达到4.0了?
  10. 如何实现wpf的多国语言
  11. 电子器件系列二十一:混频器
  12. 解决......lib/include/THC/THCGeneral.h:12:18: fatal error: cuda.h: No such file or directory报错问题
  13. 联邦学习数学公式纯手推
  14. 51学习第四天--.跟着郭老师学:程序逐渐加入模块化练习--1从流水灯1s闪烁流动--2再加数码管1~F--3.显示数码管六个灯依次显示123456--再逐渐的加入各种功能,一步一步,可训练思维!
  15. python转换excel 列号 为数字 数字转为列号
  16. 倍福EtherCAT EK1100耦合器技术参数
  17. 酷的计算机名字,微信网名最酷的名字
  18. @Deprecated
  19. java基础:运算符
  20. 软件测试肖sir__009之mysql多表(4)

热门文章

  1. input单选框多选框时可用的事件
  2. Java String 探索
  3. 手机号码验证的正则表达式(17......)
  4. C++常识“屯”和“烫”
  5. 线程安全问题的本质详解: 原子性、有序性、可见性
  6. BootStrap Table和Mybatis Plus实现服务端分页
  7. spring 整合struts
  8. 【搜索】【广搜模板】
  9. SVN和Maven及Jenkins(转)
  10. 2. Rust的三板斧 安全,迅速,并发