C#将Flat List <T>展开为Dictionary <T,ICollection <int >>

RPM*_*984 6 c# linq dictionary

我有一个List<MyClass>:

public int MyClass
{
   public int One { get; set; }
   public int Two { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

现在,数据可以(并且确实)如下所示:

一:一,二:9

一:2,二:9

一:一,二:8

一:3,二:7

看看"One"如何出现两次?我想将这个扁平序列投影到一个分组中Dictionary<int,ICollection<int>>:

KeyValuePairOne:{Key:1,Value:{9,8}}

KeyValuePairTwo:{Key:2,Value:{9}}

KeyValuePairThree:{Key:3,Value:{7}}

我猜我需要组合.GroupBy.ToDictionary

Sim*_*Var 2

var list = new List<MyClass>();

var dictionary = list.GroupBy(x => x.One)
                     .ToDictionary(x => x.Key, x => x.ToArray());
Run Code Online (Sandbox Code Playgroud)