Reputation: 1446
How could I select all information about selected item, not only first column but all ?
For the first column I just need :
ListView.Items.AddRange(ListData
.Where(i => string.IsNullOrEmpty(searchBox.Text) || i.ID.StartsWith(searchBox.Text))
.Select(c => new ListViewItem(c.ID))
.ToArray());
Lets say that next columns are : Name
, LastName
I know that I have to use Linq segment which looks like :
(...).Select(c => { })
Thanks in advance!
Upvotes: 0
Views: 1044
Reputation: 4249
You can use the ListViewItem ctor that accept an array of string (where elements after the first are the subitems)
Assuming your class has the properties LastName
and Name
ListView.Items.AddRange(ListData.Where(i =>
string.IsNullOrEmpty(searchBox.Text)
|| i.ID.StartsWith(searchBox.Text))
.Select(c => new ListViewItem // this part
(
new string[]{c.ID, c.Name, c.LastName}
)).ToArray());
If the creation of a single ListViewItem get more complicated, consider using a funcion:
ListView.Items.AddRange(ListData.Where(i =>
string.IsNullOrEmpty(searchBox.Text)
|| i.ID.StartsWith(searchBox.Text))
.Select(c => CreateListViewItemFromElement(c)).ToArray());
private ListViewItem CreateListViewItemFromElement(MyClass element)
{
// handle the element to create a "complete" ListViewItem with subitems
ListViewItem item = new ListViewItem(c.ID);
....
return item;
}
(actually, I would use the latter in every case, it's much more readable to me)
Upvotes: 1
Reputation: 205759
Well, the ListViewItem
class has 22 (!) constructor overloads, so you can use any of them that supports passing string[] items
, for instance this one:
.Select(c => new ListViewItem(new string[] { c.ID, c.Name, c.LastName }))
Upvotes: 1
Reputation: 12491
Just init all properties that you need in .Select()
method line this:
ListView.Items.AddRange(ListData.Where(i =>
string.IsNullOrEmpty(searchBox.Text)
|| i.ID.StartsWith(searchBox.Text))
.Select(c => new ListViewItem // this part
{
Name = c.ID.ToString(),
Text = c.Name + " " + c.LastName
}).ToArray());
Maby you want to fill different properties, so fill freee to change this part as you want.
Upvotes: 1