Search code examples
linqlistautocompletebox

Combining Variable Numbers of Lists w/ LINQ


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?


Solution

  • 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.