列表视图列最大值

本文关键字:最大值 视图 列表 | 更新日期: 2023-09-27 18:34:35

>我不需要排序列表视图我只需要从列中获取最大值,该列中的所有值都是数字。如果我能以某种方式将其转换为IEnumerable<int>然后我可以使用 LINQ,那就太好了。

更新

我现有的代码:DrawArray是随机数的数组。我需要在不创建另一个列表或数组的情况下获取index.ToString()列的最大值。

for (int i = 0; i < Rounds; i++)
{
ListViewItem lvItem = new ListViewItem(i.ToString());
lvItem.SubItems.Add(new ListViewItem.ListViewSubItem(lvItem, index.ToString()));
int[] DrawArray = Draw(DrawsPerRound, RoundSize);
lvItem.SubItems.Add(new ListViewItem.ListViewSubItem(lvItem, DrawArray.Aggregate("", (s, t) => s + ", " + t.ToString()).TrimStart(new char[] { ',' })));
lvItem.SubItems.Add(new ListViewItem.ListViewSubItem(lvItem, "No"));
lvItem.BackColor = Color.PaleVioletRed;
listView1.Items.Add(lvItem);
}

列表视图列最大值

可能缺少一些演员表或其他东西,而且有点丑,但这里有一个想法:

var max = listView1.Items.Cast<ListViewItem>().Max(x => int.Parse(x.SubItems[0].Text));

这将使用 LINQ,因此请确保文件中有 using System.Linq; 并且使用的是 .NET>= 3.5。

ListView.Items ListViewItemCollection继承IList, ICollection, IEnumerable

通过 linq,你可以得到你想要的。

System.Nullable<int> max = (
              from m in listView1.Items.Cast<ListViewItem>() 
              select int.ParseInt(m.SubItems[0].Text)).Max();