c# - LINQ 查询为正确的键值对使用正确的键
问题描述
我有一个列表属性,它们是这样定义的
class Attribute
{
public string Name {get; set;}
public KeyValuePair<string, object>
}
和存储在 a 中的密钥列表IList<string> listOfKeys
我想将所有值存储在对象列表中,但是如何使用 linq 查询解析正确的键,并提取值并将它们存储到list<object>
到目前为止我尝试过的东西就像
List<attribute> attributes;
some init stuff
attributes.Where(x => x.Key).
如何在这里使用我的钥匙?
解决方案
尝试使用以下组合Any
:
IList<string> listOfKeys = new List<string>();
var attributes = new List<Attribute>();
attributes.Where(a => listOfKeys.Any(l => a.Foo.Key == l));
和Attribute
类:
class Attribute
{
public string Name { get; set; }
public KeyValuePair<string, object> Foo { get; set; }
}
更新:
如果你想得到IEnumerable<object>
:
var result = attributes.Where(a => listOfKeys.Any(l => a.Foo.Key == l))
.Select(s => s.Foo.Value);
推荐阅读
- sql - 根据 id 列评估两个表中的 int 列,更新其总和
- python - 如何使用 sudo apt-get 在 Linux 上将 python 模块安装到特定的 python 版本
- string - 文本文件中的 Perl 字符串比较
- c# - 如何使用 microsoft bot 框架在瀑布模型中实现下拉列表
- ruby-on-rails - 将 content_for 与脚本标签的 src 属性一起使用?
- python - 格式字符串错误的参数过多(** 中的代码)
- bash - 值对于基础来说太大了(比较 git 提交时的错误标记
- java - Java 8 流式传输从 2 个列表中收集外部列表中记录的最佳方式
- image - 用镜头改变德鲁伊图像小部件
- c - 如何对存储在结构内的结构数组中的日期进行排序?