I am still reasonably new to C# and trying to understand list objects.
I have a list object with key/value pairs in it, populated from a database (that part is working). I then want to select a value from that list, using the 'key'. From what I have read I think Linq might be the best way to do this, however I cannot quite work out the syntax I need.
I have a list object as follows:
// Create a list of Items from the database, where Value is the 'key' and Text is the 'value'
IEnumerable<SelectListItem> ItemList = dbLists.ItemList.Select(x => new SelectListItem { Text = x.Description, Value = x.Id.ToString() });
I then want to populate another variable with the description of the selected item in the model, where the model only saves the id.
I have tried various linq Where and Select queries, but cannot work it out.
The easiest way I can think to explain what I am trying to achieve is to use sql syntax,
string SelectedItemDescription = SELECT Text FROM ItemList WHERE ItemList.Value = model.ItemCode
An example of a scenario would be something like:
ItemList
{
(Value = "1", Text = "Item 1"),
(Value = "2", Text = "Item 2"),
(Value = "3", Text = "Item 3"),
...
};
model.ItemCode = 2;
// How do I make:
SelectedItemDesctiption = "Item 2";
Hopefully that makes sense..
Thanks.