tags:

views:

238

answers:

5

I am trying to grab all unique items from a DropDownList. Is there a simple way to do this?

+1  A: 

There's always iteration:

Dim uniqueItems As New ListItemCollection()
For Each myItem As ListItem In listitems
    If Not unique.Contains(myItem) Then
        unique.Add(myItem)
    End If
Next

This is probably not the simplest answer in terms of runtime or code, but it is the simplest concept (for me, at least).

EDIT: Code for mat1t's solution

Dim uniqueValues As IEnumerable(Of ListItem) = DropDownList1.Items.Cast(Of ListItem)().Distinct() 
DropDownList1.DataSource = uniqueValues
DropDownList1.DataBind()
Matthew Jones
that assumes a Equals compare on each object.
eschneider
Looking at his other questions, I think this is what he means. He is not looking for the most programmatically correct way, he wants it to just work.
Matthew Jones
matt you are the man thank you so much for help
I__
no items show! the thing cleared itself
I__
hey matthew i am going to use your solution. help me hook this up. how do we declare "unique"??
I__
matthew jones!!!!!!!!!!!!!!!!!!!
I__
A: 

I would add them all to a hashtable to build the unique items.

eschneider
+2  A: 

Try something like this:

Dim uniqueSet As New HashSet(Of ListItem)(dropdown.Items.Cast(Of ListItem)())

This is assuming of course that you have .NET 3.5 at your disposal. The HashSet(Of T) type is a strongly-typed collection of unique items.

Andrew Hare
+3  A: 

If you're allowed to use LINQ in this code (from .net 3.5 and Visual Studio 2008 onwards) look at the Distinct extension method.

dropdown.Items.Cast(Of ListItem)().Distinct()

It will do all of the hard work of determining whether an item is unique behind the scenes

Matthew Steeples
woow!!!!! can i do this in asp.net?
I__
+1 This is much simpler!
Andrew Hare
@alex - yes you can. I think this is actually your best option.
Andrew Hare
@alex - as long as you are working in .NET 3.5 or higher, this is definitely your best shot.
Matthew Jones
i have thisDropDownList1.DataSource = dv DropDownList1.DataBind()DropDownList1.Items.Cast(Of ListItem)().Distinct()but it did not work
I__
Are you trying to only populate the listbox with unique values? If so then you want to apply the distinct filter when you set the datasource (or before)
Matthew Steeples
DropDownList1.Items.Cast(Of ListItem)().Distinct() DropDownList1.DataSource = dv DropDownList1.DataBind()didnt work
I__
yes only unique
I__
try DropDownList1.DataSource = dv.Distinct()What is dv?
Matthew Steeples
Distinct returns an object. Try this: Dim uniqueValues AS IEnumerable (OF ListItem) = DropDownList1.Items.Cast(Of ListItem)().Distinct()
Matthew Jones
Dim dv As DataView = ds.Tables("Country").DefaultView 'or we can use: 'DataView dv = ds.Tables[0].DefaultView; 'Now sort the DataView vy column name "Name" dv.Sort = "Name"
I__
Dim uniqueValues As IEnumerable(Of ListItem) = DropDownList1.Items.Cast(Of ListItem)().Distinct() DropDownList1.DataSource = dv DropDownList1.DataBind() did nto work
I__
@mat1t, it appears your solution produces an IEnumerable, which cannot bind to a DropDownList. Which sucks, cause this was very elegant.
Matthew Jones
There's another extension method on IEnumerable called ToArray (or ToList if you prefer) that will then convert it into something you can bind to a DropDownList
Matthew Steeples
+1  A: 

Hey guys i have tried all three methods described above but I keep getting returned a empty list or a list full of duplicates, i'm using a LINQ data source, so I cant alter the select statement, tried 'DISTINCT' but no luck....

Sets up the initial values for the drop down menu, works and shows lot of values

Dim context As WeldsDataContext = New WeldsDataContext()
Dim query = From BIDNumber In context.Fab_Missing_NDTs Select BIDNumber

DropDownList1.DataSource = query.ToList()
DropDownList1.DataBind()

'Should remove all none-distinct values?

Dim uniqueSet As New HashSet(Of ListItem)(DropDownList1.Items.Cast(Of ListItem)())
DropDownList1.DataSource = uniqueSet
DropDownList1.DataBind()