如何使用LINQ更改多个对象中的相同属性?

Rob*_*der 5 c# linq performance

    private void AllowOtherSelectors(bool value)
    {
        foreach (var c in this.Parent.Controls)
        {
            if (c == this)
                continue;
            if (!(c is RoundGroupedSelector))
                continue;
            var rgs = c as RoundGroupedSelector;

            rgs.AllowMultiple = value;
        }
    }
Run Code Online (Sandbox Code Playgroud)

虽然这段代码有效但我觉得它可以从使用LINQ中受益.该程序将在具有Atom处理器的平板电脑上使用,因此我只是在寻找所使用的资源/周期最少.

Jon*_*eet 11

好吧,我仍然使用foreach循环,但你可以使用LINQ作为查询部分:

foreach (var c in Parent.Controls
                        .OfType<RoundGroupedSelector>()
                        .Where(x => x != this))
{
    c.AllowMultiple = value;
}
Run Code Online (Sandbox Code Playgroud)

  • `rgs`应该是'c` (2认同)