简体   繁体   English

在Linq中列出与组相关的词典

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM