简体   繁体   中英

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 .

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. It has a Key property and implements IEnumerable<TElement> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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