Use a simple foreach
loop for update your values, LINQ
is for querying
foreach(var item in contextMenu.MenuItems.OfType<MenuItem>())
{
item.Enabled = someValue;
}
Question
I have a context menu. I went to iterate through all the menu items and disable or enable them based on a some boolean value.
This is what I have so far, but it doesn't work:
contextMenu.MenuItems.Cast<MenuItem>().Select(x =>
{
x.Enabled = someValue;
return x;
});
Solution
Use a simple foreach
loop for update your values, LINQ
is for querying
foreach(var item in contextMenu.MenuItems.OfType<MenuItem>())
{
item.Enabled = someValue;
}
OTHER TIPS
The closest thing is the ForEach
method on List<T>
. I don't this that it's technically considered LINQ and unlike the core LINQ to Objects methods it's not part of IEnumerable<T>
. Anyway, I'm guessing you'll prefer it to a traditional foreach
loop since it looks like any other LINQ query using method syntax.
contextMenu.MenuItems.Cast<MenuItem>().ToList().ForEach(x => x.Enabled = someValue);