Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
How can we transform a list of string or a list of object to a ListViewItemCollection in one line with linq, where object is for example a person
How can we transform a list of string or a list of object to a ListViewItemCollection in one line with linq, where object is for example a person with Name property that will be displayed to the ListViewItem.
Here is my current code :
foreach (string word in sf.lstWords) { lvWords.Items.Add(new ListViewItem(word)); }
Use ListView.ListViewItemCollection.AddRange and the Linq method Select
ListView.ListViewItemCollection.AddRange
Select
lvWords.Items.AddRange(sf.lstWords.Select(t => new ListViewItem(t)).ToArray());
I use ToArray() because the signature of AddRange is void AddRange(ListViewItem[])
ToArray()
AddRange(ListViewItem[])
使用ListView.ListViewItemCollection.AddRange和Linq方法Select
我使用ToArray()是因为 AddRange 的签名是无效的AddRange(ListViewItem[])