I need to select a number of values (into a List) from a Dictionary based on a subset of keys.
I\'m trying to do this in a single line of code using Linq but what I have
A Dictionary
is IEnumerable
, so you can simply Select
the Value
property:
List selectedValues = dictionary1
.Where(x => keysToSelect.Contains(x.Key))
.Select(x => x.Value)
.ToList();
or
var selectValues = (from keyValuePair in dictionary1
where keysToSelect.Contains(keyValuePair.Key)
select keyValuePair.Value).ToList()