Background: I have an ASP.NET MVC view page with a MultiSelectList in the View Model. I want to populate a label with the list of SelectedValues from that MultiSelectList object. The list is stored within the MultiSelectList with a type of IDName:
public class IDName {
public int ID {get; set;}
public string Name {get; set;}
}
So MultiSelectList.Items is an IEnumerable containing a list of IDName's. I can do a foreach over this list and grab .Name to get the name of each entry. But, I only want to do this if the ID is in MultiSelectList.SelectedItems, which appears to be a string[] and looks like:
["1", "3", "4"]
So, I want to just get the IDName.Name when IDName.ID is in the SelectedItems list.
I'm pretty new to both MVC and C# so I'm not sure the best way to do that. Any suggestions?
Update #2:
OK, I was being dense. This works:
list.Items
.Cast<IDName>()
.Where(x => list.SelectedValues.Cast<string>().Contains(x.ID.ToString()))
SelectedValues is actually an IEnumerable containing a string[].