【问题标题】:Dictionary with single key multiple values in a list列表中具有单键多个值的字典
【发布时间】:2015-08-19 22:46:40
【问题描述】:

我有一个要放入字典的 linq 查询。我的字典是

Dictionary<string, List<string>>

如何将下面的查询放入定义的字典中?

...
...
select new
   {
     b.PropertyName             
     a.PropertyValue,
     a.PropertyOrder,
   }).Distinct().ToDictionary(x => x.PropertyName, 
    x => x.ToList() // The rest of the two values should be in the list
    );

【问题讨论】:

标签: c# linq


【解决方案1】:
select new {
b.PropertyName
a.PropertyValue,
a.PropertyOrder})
.Distinct()
.ToDictionary(key => key.PropertyName, 
value => new List<string>{ value.PropertyValue, value.PropertyOrder});

【讨论】:

  • 你能解释一下为什么会这样吗?在 SO 上不鼓励仅使用代码回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-22
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 2019-04-28
  • 1970-01-01
相关资源
最近更新 更多