转换.net 2 app的Linq代码 [英] Convert Linq code for .net 2 app

查看:70
本文介绍了转换.net 2 app的Linq代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi
在本文 中,一种方法使用Linq表达式。如何在没有Linq的情况下转换在.net 2中可用的方法?



方法:



< pre lang =xml> private static IEnumerable < Control > _get_all_controls(对照c)
{
返回c.Controls.Cast < Control > ()。SelectMany(item =>
_get_all_controls(item))。Concat(c.Controls.Cast < 控制 > ()) .Where(control =>
control.Name!= string.Empty);
}

解决方案

您必须查看每个Linq方法的eth文档,并用等效的方法替换它们foreach循环。



我们不会为你这样做:我们希望你自己至少完成一些工作。你到目前为止所做的一切(看起来似乎)都找到了一些可能做你想做的代码,并要求我们做你想做的事。



它是很明显该代码的作用:它返回一个IEnumerable< T>控件的集合,其中包括嵌套在Controls集合顶层的控件。

这样做:它只有一个嵌套循环!


此代码返回一个表格中的控件列表:



  private 列表< control> _get_all_controls(对照c)
{
List< control> lstC = new List< control>();

foreach (控制ctrl c.Controls)
{
lstC.Add(ctrl);

lstC.AddRange(_get_all_controls(ctrl));
}
return lstC;
} < / control > < / control > < / control >


Hi In this article, a method use Linq expressions. How convert this method that be usable in .net 2 without Linq?

The method:

private static IEnumerable<Control> _get_all_controls(Control c)
    {
        return c.Controls.Cast<Control>().SelectMany(item =>
            _get_all_controls(item)).Concat(c.Controls.Cast<Control>()).Where(control =>
            control.Name != string.Empty);
    }

解决方案

You will have to look at eth documentation for each of the Linq methods, and replace them with the equivalent foreach loop.

We don't do that for you: we expect you to do at least some of the work yourself. All you have done so far (it would seem) is find some code that might do what you want, and ask us to make it do what you want.

It's pretty obvious what that code does: it returns a IEnumerable<T> collection of controls, which includes controls nested in the top level of the Controls collection.
So do it: it's only one nested loop!


This code return a list of controls in a form:

private List<control> _get_all_controls(Control c)
    {
        List<control> lstC = new List<control>();

        foreach (Control ctrl in c.Controls)
        {
            lstC.Add(ctrl);

            lstC.AddRange(_get_all_controls(ctrl));
        }
        return lstC;
    }</control></control></control>


这篇关于转换.net 2 app的Linq代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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