LINQ Group By into a Dictionary Object

前端 未结 4 390
野的像风
野的像风 2021-01-30 02:07

I am trying to use LINQ to create a Dictionary> from a List. I can get this to work using \"

4条回答
  •  鱼传尺愫
    2021-01-30 02:23

    I cannot comment on @Michael Blackburn, but I guess you got the downvote because the GroupBy is not necessary in this case.

    Use it like:

    var lookupOfCustomObjects = listOfCustomObjects.ToLookup(o=>o.PropertyName);
    var listWithAllCustomObjectsWithPropertyName = lookupOfCustomObjects[propertyName]
    

    Additionally, I've seen this perform way better than when using GroupBy().ToDictionary().

提交回复
热议问题