views:

16

answers:

2

I have a list (List) of objects. Each of those objects contains a list (List) of strings describing them.

I'm needing to create a dropdown containing all of the distinct strings used to describe the objects (Cards). To do this, I need a list of distinct strings used.

Any idea how/if this can be done with LINQ?

+2  A: 

You can use the SelectMany extension method/operator to flatten a collection into the individual elements.

listOfObjects.SelectMany(x => x.DescriptionStrings).Distinct()

This will select all the strings out of the collection of description strings for each object in your list of objects.

womp
A: 

LINQ has a Distinct function.

Assuming "_cards" exists as instance variable of List and Card.Descriptions returns the descriptions and "cardsComboBox" (in WinForms):

cardsComboBox.AutoCompleteSource = _cards.SelectMany(c => c.Descriptions).Distinct();

A reminder that that will be the list of card descriptions at the time of binding however. If you want to keep it synchronised when _cards get updated then you'll have to do some more fancy footwork or look at a reactive binding source. (We use Bindable.Linq)

Reddog