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):

  • Key: "string1", Value: "One"
  • Key: "string2", Value: "Two"
  • Key: "string3", Value: "Three"

What is the best way to get such result?

Edit
Report