c# - C# 字典过滤 (LINQ) 和列表扩展方法过滤值并获取密钥
问题描述
我有一本字典fooDictionary<string, MyObject>
。
我正在过滤fooDictionary
以仅获取具有特定属性值的 MyObject 。
fooDictionary.Values.Where(x=>x.Boo==false).ToList().ExtensionMethod();
//(扩展方法是我为列表制作的扩展方法(用于更多过滤)(PS:ExtensionMethod仅返回1x MyObject))但我也想获取已经过滤的MyObject的键。我怎样才能做到这一点?
解决方案
When you operate on the dictionary values with fooDictionary.Values.Where(...)
, you no longer have access to the keys.
instead change the query to:
fooDictionary.Where(x => x.Value.Boo == false).ToList().ExtensionMethod();
After the ToList()
call, this should yield a List<KeyValuePair<string, MyObject>>
hence maintaining both the key as well as the corresponding value.
推荐阅读
- telegram - 通过内联键盘 Telegram VB.NET 更新当前消息
- c++ - 创建指针会占用大量资源吗?
- python - 通用关系没有反映在 django 的迁移中?
- java - 在 Spring 中覆盖库版本
- c - 关于 sizeof 溢出 size_t 的 C 标准
- excel - 运行时错误,用于电子邮件自动化的 VBA EXCEL
- php - PHP中的Excel数据显示自动显示一组行
- python - 使用Event在线程中循环时如何修复Window对象没有属性_stop_thread?
- openssl - 在 Windows 上构建静态 OpenSSL 库
- python - 使用 NetworkX 查找从一个到另一个的最短和最长权重