如何转换列表<串GT;或列表<对象>在一个行ListView.ListViewItemCollection [英] How to convert a list<string> or list<object> to a ListView.ListViewItemCollection in one line

查看:327
本文介绍了如何转换列表<串GT;或列表<对象>在一个行ListView.ListViewItemCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何可以改变的字符串列表或对象的一个​​ListViewItemCollection列表一行使用LINQ,其中目的例如与将显示在ListViewItem的Name属性的人。



下面是我当前的代码:

 的foreach(字符串字sf.lstWords)
{
lvWords.Items.Add(新的ListViewItem(字));
}


解决方案

使用的 ListView.ListViewItemCollection.AddRange 和LINQ的方法的 选择

  lvWords.Items.AddRange(sf.lstWords.Select(T =方式>新的ListViewItem(t))的ToArray的()); 



我用 ToArray的()因为签名的的AddRange是无效的的AddRange(ListViewItem的[])


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

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

I use ToArray() because the signature of AddRange is void AddRange(ListViewItem[])

这篇关于如何转换列表<串GT;或列表<对象>在一个行ListView.ListViewItemCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆