[英]Return ListBox with the lowest item count using LINQ
我需要在表單中找到最少項目的表單上找到一個六個列表框。
如何將ListBox.Item.Count
屬性與LINQ的擴展方法Min()
結合使用,以返回具有最少項目數的ListBox
?
我嘗試了以下操作,但是它無法正常工作:
foreach (var item in Controls.OfType<ListBox>())
{
if (item.Items.Count <= min)
{
listbox = item;
min = listbox.Items.Count;
}
}
這應該可以解決問題:
ListBox b = Controls
.OfType<ListBox>()
.FirstOrDefault(lb => lb.Items.Count == listboxes.Min(l => l.Items.Count));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.