I have a DataTable that contains a multi-select choice field (string). Values in the field are stored like:
Charlie
Alpha
Alpha; Charlie; Delta
Bravo; Charlie
Bravo
Alpha; Bravo; Charlie
I’m trying to get the unique list to display in a dropdown:
Alpha
Bravo
Charlie
Delta
Method to split the values and return a List<string>:
private static List<string> GetValues(string multiValueString)
{
string[] delimiters = {"; "};
string[] values = multiValueString.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
return values.ToList<string>();
}
I was trying to use LINQ to get the unique values… but can’t quite figure it out. I thought I could use dataTable.AsEnumerable(), do a select like .Select(r => GetValues(r.Field<string>("Brand"))) and then Union the results somehow.
But I can’t quite get it working.. what am I doing wrong?
You need to use
SelectManyasGetValueswill return mulitple items (i.e. anIEnumerable) for each row.SelectManyflattens these enumerables into a single enumerable of the resultant type:The distinct removes the duplicate entries.