从C#泛型字典筛选出价值 [英] Filtering out values from a C# Generic Dictionary
本文介绍了从C#泛型字典筛选出价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个C#字典,词典< GUID,可为MyObject>
,我需要的基础上的财产要被过滤为MyObject
。
I have a C# dictionary, Dictionary<Guid, MyObject>
that I need to be filtered based on a property of MyObject
.
例如,我想从字典中,其中 MyObject.BooleanProperty = FALSE
删除所有记录。什么是acheiving本的最佳方式?
For example, I want to remove all records from the dictionary where MyObject.BooleanProperty = false
. What is the best way of acheiving this?
推荐答案
由于字典工具的IEnumerable&LT; KeyValuePair&LT;关键字,值&GT;&GT;
,你可以只使用其中:
Since Dictionary implements IEnumerable<KeyValuePair<Key, Value>>
, you can just use where:
var matches = dictionary.Where(kvp => !kvp.Value.BooleanProperty);
要重新如果你需要它,使用 ToDictionary
方法一个新的字典。
To recreate a new dictionary if you need it, use the ToDictionary
method.
这篇关于从C#泛型字典筛选出价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文