I have the below model which represents a two level menu hierarchy and what I need to do is select one ID from the SubMenuItems sub collection then concatenate the results from its and its parent records TextToDisplay properties.
The question I have is how to return a single parent item and a single sub item then access the TextToDisplay property?
The object that I want to query is an ObserableCollection
public ObservableCollection<MenuModel> MenuItems { get; set; }
Model
public class MenuModel
{
public int ID { get; set; }
public string TextToDisplay { get; set; }
public string ImageSource { get; set; }
public ObservableCollection<MenuModel> SubMenuItems { get; set; }
}
Use
Single()to get one item, andAny()to check if there is an element of a list which matches a query:Note this will throw an exception if no parent item contains a sub item with the ID of 30, or if more than one parent item is returned – so you may need to use
SingleOrDefault()instead and check for null, orWhereand then choose which item you want.