【问题标题】:c# List<KeyValuePair> Get Value by Key Or Index [duplicate]c# List<KeyValuePair> 通过键或索引获取值[重复]
【发布时间】:2018-03-01 10:12:11
【问题描述】:

如何通过key为keyKeyValuePair获取value

我有一个List&lt;KeyValuePair&lt;string, string&gt;&gt;

var dataList = new List<KeyValuePair<string, string>>();

// Adding data to the list
dataList.Add(new KeyValuePair<String, String>("name", "foo"));
dataList.Add(new KeyValuePair<String, String>("name", "bar"));
dataList.Add(new KeyValuePair<String, String>("age", "24"));

为该列表创建一个循环:

foreach (var item in dataList) {
    string key = item.Key;
    string value = item.Value;
}

我想做的是以某种方式获得string name = item["name"].Value

foreach (var item in dataList) {
    // Print the value of the key "name" only
    Console.WriteLine(item["name"].Value);

    // Print the value of the key "age" only
    Console.WriteLine(item["age"].Value);
}

或者也许通过 Index 获得 Value,例如 Console.WriteLine(item[0].Value)

我怎样才能做到这一点?

注意: 我只需要使用一个 foreach,而不是为每个键使用单独的 foreach。

编辑 1 如果我使用 if(item.Key == "name") { // do stuff } 我将无法使用该 if 语句中的其他键,所以我需要按照这个逻辑工作:

if(item.Key == "name") {
    // Print out another key
    Console.WriteLine(item["age"].Value)

    // and that will not work because the if statment forced to be the key "name" only
}

Edit 2 我尝试使用 Dictionary 并向其中添加数据,例如:

dataList.Add("name", "john");
dataList.Add("name", "doe");
dataList.Add("age", "24");

上面写着An item with the same key has already been added.,我想是因为我要使用相同的键"name" 添加多个项目,我需要这样做。

编辑 3 我要达到的目标instead of how i try to do it

我正在尝试遍历 List 并确定是否存在带有关键 path 文件的项目,如下所示:

if(File.Exists(item["path"]) { Console.WriteLine(item["name"]) }

// More Explained

foreach (var item in dataList) {
    if (File.Exists(//the key path here//)) {
        MessageBox.Show("File //The key name here// exists.");
    }else {
        MessageBox.Show("File //The key name here// was not found.");
    }
}

我不能那样使用 item["path"] 的问题.. 我能做的就是 item.Key & item.Value

【问题讨论】:

  • 你有一个List 而不是Dictionary&lt;string, string&gt; 有什么原因吗?您的按键搜索用例使您看起来实际上并不需要该列表。
  • 你能举一个使用Dictionary&lt;string, string&gt;的例子吗,因为我试过用它,但它说有更多的项目有相同的键?我想拥有多个具有相同密钥的项目。如果可以的话,我的问题将得到解决。
  • 在这一点上,这看起来真的很像XY problem。你为什么不退后一步,准确地解释你想做什么 而不是 你是怎么做的?
  • 我已经更新了问题,请问您可以阅读 Edit 3 部分吗?
  • 键名是灵活的还是固定的?

标签: c# linq keyvaluepair


【解决方案1】:

您可以仅通过所需的键运行foreach 查询:

foreach ( var item in dataList.Where( i => i.Key == "name" ) )
{
    //use name items
}

这使用 LINQ 仅包含 KeyValuePairs,其中 Key"name"。您必须将using System.Linq 添加到源代码文件的顶部才能正常工作。

【讨论】:

  • 我在问题中添加了注释,请阅读。
  • 我不明白这个问题。如果你只想使用一个foreach,那么每次你只会得到一个项目,所以你可以做类似if ( item.Key == "name" ) ... else ...的事情?
  • if(item.Key == "name") { Console.WriteLine(another key) } 如果我强制它键入“名称”.. 内含我无法获得其他键我需要在同一个foreach 中使用多个键你明白吗?跨度>
  • 问题是“姓名”和“年龄”之间没有关系。 dataList 只是一个值列表。第一个是“名字”,第二个是“名字”,第三个是“年龄”。它们之间没有“联系”。如果你想以某种方式关联它们,你应该创建一个具有NameAge 属性的自定义类,然后列出这些属性。
  • 请阅读编辑3部分问题。
猜你喜欢
  • 1970-01-01
  • 2018-01-07
  • 1970-01-01
  • 1970-01-01
  • 2018-01-06
  • 2021-02-12
  • 1970-01-01
  • 2011-04-27
  • 2012-12-15
相关资源
最近更新 更多