How to select multiple values from a Dictionary using Linq as simple as possible

Well you could start from the list instead of the dictionary:

var selectedValues = keysToSelect.Where(dictionary1.ContainsKey)
                     .Select(x => dictionary1[x])
                     .ToList();

If all the keys are guaranteed to be in the dictionary you can leave out the first Where:

var selectedValues = keysToSelect.Select(x => dictionary1[x]).ToList();

Note this solution is faster than iterating the dictionary, especially if the list of keys to select is small compared to the size of the dictionary, because Dictionary.ContainsKey is much faster than List.Contains.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)