1

我有一个通用方法,它使用一系列有效的 foreach 循环将页面上的所有 html 控件添加到通用列表中。是否可以将其转换为 LINQ 表达式?

private List<T> GetControls<T>() where T : HtmlControl
         {
            List<T> c = new List<T>();

            foreach (HtmlControl c1 in Controls)
            {
                foreach (HtmlControl c2 in c1.Controls)
                {
                    if (c2.GetType() == typeof(HtmlForm))
                    {
                        foreach (Control c3 in c2.Controls)
                        {
                            if (c3.GetType() == typeof(ContentPlaceHolder))
                            {
                                foreach (HtmlControl c4 in c3.Controls)
                                {
                                    if (c4.GetType() == typeof(T))
                                    {
                                        c.Add((T)c4);
                                    }
                                    if (c4.GetType() == typeof(PlaceHolder))
                                    {
                                        foreach (HtmlControl c5 in c4.Controls)
                                        {
                                            if (c5.GetType() == typeof(T))
                                            {
                                                c.Add((T)c5);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return c;
        }
4

2 回答 2

1

这应该这样做:

List<T> c = this.Controls.Cast<Control>()
    .SelectMany(c1 => c1.Controls.Cast<Control>())
    .OfType<HtmlForm>()
    .SelectMany(c2 => c2.Controls.Cast<Control>())
    .OfType<ContentPlaceHolder>()
    .SelectMany(c3 => c3.Controls.Cast<Control>())
    .SelectMany(c4 =>
        {
            if (c4 is T)
                return new[] { (T)c4 };
            if (c4 is PlaceHolder)
                return c4.Controls.Cast<Control>().OfType<T>();
            return Enumerable.Empty<T>();
        })
    .ToList();

但是请注意,我在is这里使用了类型比较而不是类型比较。这是故意的,因为这也是OfTypeLINQ 方法在内部使用的。

如果您确定要精确类型而不是通过is比较的对象,则必须实现自己的OfType(或仅使用.Where(x => x.GetType == typeof(whatever)))。

(另请注意,我使用Control了代替HtmlControl,以防您的某些HtmlControls 包含常规Controls。)

于 2012-09-20T11:47:10.163 回答
0

你可以尝试这样的事情:

 private List<T> GetControls<T>() where T : HtmlControl
    {
        List<T> c = new List<T>();

        foreach (HtmlControl c4 in from HtmlControl c1 in Controls
                                   from HtmlControl c2 in c1.Controls
                                   where c2.GetType() == typeof (HtmlForm)
                                   from Control c3 in c2.Controls
                                   where c3.GetType() == typeof (ContentPlaceHolder)
                                   from HtmlControl c4 in c3.Controls
                                   select c4)
        {
            if (c4.GetType() == typeof(T))
            {
                c.Add((T)c4);
            }
            if (c4.GetType() == typeof(PlaceHolder))
            {
                c.AddRange(from HtmlControl c5 in c4.Controls where c5.GetType() == typeof (T) select (T) c5);
            }
        }
        return c;
    }

虽然我不确定它是否更优雅......

于 2012-09-20T11:36:38.260 回答