【问题标题】:Add Item to List<KeyValuePair<Object, List<Object>>>将项目添加到 List<KeyValuePair<Object, List<Object>>>
【发布时间】:2016-01-21 00:27:45
【问题描述】:

我正在使用包含一个对象和另一个列表的 KeyValuePair 创建以下列表:

var testList = new List<KeyValuePair<classTypeObject, List<DataTypeObject>>>
{
    new KeyValuePair<classTypeObject, List<DataTypeObject>>(classTypeObject.Mountain, new List<DataTypeObject>()),
    new KeyValuePair<classTypeObject, List<DataTypeObject>>(classTypeObject.City, new List<DataTypeObject>()),
};

如何将项目添加到列表中,例如 Key=classTypeObject.City 的第二个条目?

【问题讨论】:

  • 您可以通过索引testList[1] 解决它,除非您正在寻找更通用的解决方案。

标签: c# .net list keyvaluepair


【解决方案1】:

应该是:

testList[1].Value.Add(....)

值 = 列表 Key = classTypeObject

如果您的密钥稍微复杂一点,您可以 使用 lambda 表达式:

testList.Where(x => x.Key == classTypeObject.City).First().Value.Add(...)

【讨论】:

  • 抱歉,只见树木不见森林。谢谢
  • 没问题。很高兴我能帮上忙。
猜你喜欢
  • 2018-07-17
  • 1970-01-01
  • 2014-05-13
  • 1970-01-01
  • 2021-01-21
  • 2022-07-26
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
相关资源
最近更新 更多