c# - Linq 获取所有控件(有序)

标签 c# .net linq .net-3.5

有没有办法使用 linq 获取所有控件。

我想做的是类似的事情(按选项卡索引对控件进行排序):

foreach (Control control in this.Controls.OrderBy(c => c.TabIndex)
{
    ...
}

当我得到一个列表时我使用这种查询<...>

我使用 c# 和 .Net 3.5

最佳答案

ControlCollection仅实现 IEnumerable ,不是IEnumerable<T> 。不过,这很容易修复 - 添加对 Cast() 的调用:

foreach (Control control in Controls.Cast<Control>()
                                    .OrderBy(c => c.TabIndex))
{
}

或者您可以使用查询表达式,它将调用 Cast()必要时:

var controls = from Control c in Controls
               orderby c.TabIndex
               select c;

foreach (Control control in controls)
{
}

关于c# - Linq 获取所有控件(有序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/627730/

相关文章:

c# - 无法将 List<List<int>> 转换为返回类型 IList<IList<int>>

c# - 将带有句点的对象列表组合成唯一列表

c# - 静态检测 linq 表达式是否为 null

c# - 如何将参数传递给 Activator.CreateInstance<T>()

c# - 减少 Web 应用程序中的全局状态

C# 生成查询以将 DateTime 与 SQL Nvarchar 日期列进行比较

c# - 将代码标记为在设计时不运行

c# - 什么是 Winforms 中的 parking 窗口

.net - 我们可以将 WEBSITE_LOAD_USER_PROFILE=1 添加到 web.config 中吗

c# - Entity Framework - 包含的导航属性的选择条件