Group into a dictionary of elements

前端 未结 1 1950
后悔当初
后悔当初 2021-02-03 18:47

Hi I have a list of element of class type class1 as show below. How do I group them into a

Dictionary> based on the group

相关标签:
1条回答
  • 2021-02-03 18:58

    It will be more efficient to replace:

    gdc => gdc.ToList()[0].groupID
    

    with:

    gdc => gdc.Key
    

    Other than that, it looks like I would have done.

    Alternately, if you are okay with extension methods over LINQ (I personally prefer them), it can be shortened further still with:

    var t = data.GroupBy(data => data.groupID).ToDictionary(gdc => gdc.Key, gdc => gdc.ToList());
    
    0 讨论(0)
提交回复
热议问题