[英]List to Dictionary with Group in Linq
I have the following variable: 我有以下变量:
List<Tuple<DataType, String, List<String>>> items;
I need to create a Dictionary<DataType, String[]>
where the DataType
is the Tuple's Item1 and the String[]
are all the Tuple's Items 2 for that DataType
. 我需要创建一个
Dictionary<DataType, String[]>
,其中DataType
是Tuple的Item1, String[]
是该DataType
所有Tuple的Items 2。
So I tried: 所以我尝试过:
Dictionary<DataType, String[]> d = items.GroupBy(x => x.Item1)
.ToDictionary(x => x.Key, x => x.Value);
This does not compile. 这不编译。
How can I solve this? 我怎么解决这个问题?
Dictionary<DataType, String[]> d = items
.GroupBy(x => x.Item1)
.ToDictionary(
g => g.Key,
g => g.Select(t => t.Item2).ToArray()); //the change is on this line
The IGrouping<TKey, TElement>
type doesn't have a Value
member. IGrouping<TKey, TElement>
类型没有Value
成员。 It has a Key
property and implements IEnumerable<TElement>
. 它具有
Key
属性并实现IEnumerable<TElement>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.