簡體   English   中英

使用linq查詢ASP.NET表單對象

[英]Querying the ASP.NET form object with linq

是否可以選擇所有使用linq繼承特定抽象類的控件。

利用Enumerable.OfType<T>擴展方法:

...Form.Controls.OfType<MyControlBase>()...

要遞歸執行此操作,請參見另一個答案 :創建一個迭代器,該迭代器遍歷控制樹以創建一個列表,然后將LINQ運算符應用於該列表。

假設您有一個遞歸獲取所有控件的方法,如下所示:

public static IEnumerable<Control> GetControls(Control form) {
    foreach (Control childControl in form.Controls) {   // Recurse child controls.
        foreach (Control grandChild in GetControls(childControl)) {
            yield return grandChild;
        }
        yield return childControl;
    }
}

然后,您可以執行以下操作:

var controls = GetControls(form).OfType<SomeControlBase>();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM