我想过滤掉一些我不需要进一步处理的字典对.检查此示例代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,onlyStrings是a Dictionary<string, KeyValuePair<string, object>>
但我希望只有字符串具有以下对(源字典的子集):
获得这样结果的最佳方法是什么?
Dan*_*ale 28
ToDictionary方法有一个重载,它也允许一个elementSelector委托:
var onlyStrings = source.Where(s => s.Key != "number")
.ToDictionary(dict => dict.Key, dict => dict.Value);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8629 次 |
| 最近记录: |