1

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.

1 Answer 1

3

you can do like this in LINQ, if you write it this way:

string SelectedItemDescription = (from list in ItemList 
                                 where list.Value = model.ItemCode
                                 select list.Text).FirstOrDefault();

or you can Use Extension Methods, If it will always return a single element then you can use SingleOrDefault():

SelectListItem SelectedItemDescription = ItemList.SingleOrDefault(item=>item.Value == model.ItemCode);

if(SelectedItemDescription !=null)
    {        

        string Key =  SelectedItemDescription .Value;
        string Text = SelectedItemDescription.Text;
    }

If it will return a multiple objects then you can use FirstOrDefault():

SelectListItem SelectedItemDescription = ItemList.FirstOrDefault(item=>item.Value == model.ItemCode);

if(SelectedItemDescription !=null)
{        

    string Key =  SelectedItemDescription .Value;
    string Text = SelectedItemDescription.Text;
}

UPDATED:

As @CodeGeek suggested in comments, you can also do like this:

string SelectedItemDescription =ItemList
                                .SingleOrDefault(item=>item.Value == model.ItemCode)!=null ? ItemList.SingleOrDefault(item=>item.Value == model.ItemCode).Text : String.Empty;
Sign up to request clarification or add additional context in comments.

8 Comments

Your code will throw a null reference exception if that item is not in the list
string SelectedItemDescription = ItemList.SingleOrDefault(item=>item.Value == model.ItemCode).Text; will do it straight
@DavidPilkington i am using SingleOrDefault
@EhsanSajjad from MSDN on SingleOrDefault "a default value if the sequence is empty" the default of his object is most likely null
yes you are right i added null check @DavidPilkington updated
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.