ToastedCrack
ToastedCrack

Reputation: 3

C# - How to get the value instead of id's in a List

I have a List containing objects. Each object holds an 'mta' Id

List<Elements.ReservationElement> reservationList = new List<Elements.ReservationElement>();

How can I get the corresponding value (instead of the id), without using a loop like:

List<String> valueList = new List<String>();
foreach (var resInList in reservationList) {
  valueList.Add(context.mtas.FirstOrDefault(id => id.mta_id == resInList.mta_id).mta_name);
}

I think this question already might be asked - but haven't found anything after searching for hours... thank you in advance!

Upvotes: 0

Views: 70

Answers (1)

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101701

You are looking for join

var valueList = (from r in reservationList 
                 join m in context.mtas on r.mta_id equals m.mta_id
                 select r.mta_name).ToList();

Upvotes: 3

Related Questions