I want to filter out some dictionary pairs I do not need for further processing. Check this sample code out:
static void Main(string[] args) { var source = new Dictionary<string, dynamic>(); source.Add("number", 1); source.Add("string1", "One"); source.Add("string2", "Two"); source.Add("string3", "Three"); var onlyStrings = source.Where(s => s.Key != "number").ToDictionary(s => s.Key); }
In this case, onlyStrings is a Dictionary<string, KeyValuePair<string, object>>
but I want onlyStrings to have the following pairs (a subset of the source dictionary):
What is the best way to get such result?
There is an overload to the ToDictionary method that also allows for an elementSelector delegate:
var onlyStrings = source.Where(s => s.Key != "number") .ToDictionary(dict => dict.Key, dict => dict.Value);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With