LINQ 分组为字典对象

问题描述

Dictionary<string, List<CustomObject>> myDictionary = listofCustomObjects
    .GroupBy(o => o.PropertyName)
    .ToDictionary(g => g.Key, g => g.ToList());

解决方法

我正在尝试使用 LINQDictionary<string,List<CustomObject>>List<CustomObject>.
我可以使用“var”让它工作,但我不想使用匿名类型。这是我所拥有的

var x = (from CustomObject o in ListOfCustomObjects
      group o by o.PropertyName into t
      select t.ToList());

Cast<>()一旦我拥有了 LINQ 库,我也尝试过使用它x,但是我遇到了编译问题,因为它是无效的强制转换。