Reputation: 299
I need to find a ListBox of six on a Form with the lowest number of items in it.
How can I use the ListBox.Item.Count
property in conjunction with the extension method Min()
from LINQ for returning the ListBox
with the lowest item count?
I tried the following, but it's not working as I want it to:
foreach (var item in Controls.OfType<ListBox>())
{
if (item.Items.Count <= min)
{
listbox = item;
min = listbox.Items.Count;
}
}
Upvotes: 0
Views: 84
Reputation: 3512
This should do the trick:
ListBox b = Controls
.OfType<ListBox>()
.FirstOrDefault(lb => lb.Items.Count == listboxes.Min(l => l.Items.Count));
Upvotes: 2