gpt4 book ai didi

c# - 如何在一行中将 list 或 list 转换为 ListView.ListViewItemCollection
转载 作者:行者123 更新时间:2023-11-30 19:10:53 24 4
gpt4 key购买 nike

我们如何使用 linq 在 一行 中将字符串列表或对象列表转换为 ListViewItemCollection,其中对象是例如将显示到 ListViewItem 的具有 Name 属性的人。

这是我当前的代码:

foreach (string word in sf.lstWords)
{
lvWords.Items.Add(new ListViewItem(word));
}

最佳答案

使用ListView.ListViewItemCollection.AddRange和 Linq 方法 Select

lvWords.Items.AddRange(sf.lstWords.Select(t => new ListViewItem(t)).ToArray());

我使用 ToArray() 因为 AddRange 的签名是 void AddRange(ListViewItem[])

关于c# - 如何在一行中将 list<string> 或 list<object> 转换为 ListView.ListViewItemCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14676374/

24 4 0