我在一个列表中有以下对象:
public class DemoClass
{
public int GroupKey { get; set; }
public string DemoString { get; set; }
public object SomeOtherProperty { get; set; }
}
现在,我想用它来创建以下字典:
Dictionary<int, List<DemoClass>>
我想按属性 GroupKey
对 List<DemoClass>
进行分组,但是我不明白这是如何做到的,需要一些帮助。
经过一番思考,我达到了需要的行为:
var groupedDemoClasses = from demoClass in mySepcialVariableWhichIsAListOfDemoClass
group demoClass by demoClass.GroupKey
into groupedDemoClass
select groupedDemoClass;
var neededDictionary = groupedDemoClass.ToDictionary(gdc => gdc.Key, gdc => gdc.ToList());
but, is there a way to make this into a single statement?