LINQ Distinct annonomous ListViewItem "text" query selecting ListViewItem index

ident

Member
Joined
Apr 30, 2012
Messages
12
Location
Cambridge
Programming Experience
3-5
LINQ Distinct annonomous ListViewItem "text" query selecting ListViewItem index

Trying to cast a ListView collection distinct to return the items index, I assume i will need to cast the items as string to get the text property but then i will loose the listview collection.

VB.NET:
[COLOR=#2fcefe]Dim[/COLOR] items = (             
[COLOR=#2fcefe]From[/COLOR] item [COLOR=#2fcefe]
In[/COLOR] [COLOR=#2fcefe]Me[/COLOR].NamesListView.Items.Cast([COLOR=#2fcefe]Of[/COLOR] [COLOR=#2b91af]ListViewItem[/COLOR])().Distinct             [COLOR=#2fcefe]
Select[/COLOR] item.Index             
).ToArray
 
Which of the items that have same Text is it you want to select?
There are several ways you can do this, for example use the Distinct overload that takes the comparer parameter, or Group By and select one item from each group. Latter of these two is least coding and also the one that would let you control which item in group to take, for example first.
 
Ok, going on LINQ's Group by

VB.NET:
[COLOR=#2fcefe]Dim[/COLOR] items = ([COLOR=#2fcefe]
From[/COLOR] item [COLOR=#2fcefe]
In[/COLOR] [COLOR=#2fcefe]Me[/COLOR].NamesListView.Items                      [COLOR=#2fcefe]
Group[/COLOR] item [COLOR=#2fcefe]
By[/COLOR] item.text                      
[COLOR=#2fcefe]Into[/COLOR]                      
[COLOR=#2fcefe]Group[/COLOR]                      
[COLOR=#2fcefe]Select[/COLOR] [COLOR=#6464b9]...[/COLOR]..                      
).ToArray()

So i need to select One item from each group, but even if i work this to do that surely it's causing issues converting from a listviewitem to string because i then will need to return it's listview index. Maybe grouping by count will return the distinct items.
 
Dim items = (From item In Me.ListView1.Items.Cast(Of ListViewItem)() Group By item.Text Into Group Select Group.First.Index).ToArray
 
Back
Top