LINQ - Convert List to Dictionary with Value as List

前端 未结 2 1799
情深已故
情深已故 2020-12-23 13:08

I have a

List 

that I retrieve from the database. However, I would like it keyed by a property in MyObject for grouping pu

2条回答
  •  鱼传尺愫
    2020-12-23 13:35

    It sounds like you want to group the MyObject instances by KeyedProperty and put that grouping into a Dictionary>. If so then try the following

    List list = ...;
    var map = list
      .GroupBy(x => x.KeyedProperty)
      .ToDictionary(x => x.Key, x => x.ToList());
    

提交回复
热议问题