LINQ query to return a Dictionary

前端 未结 2 1110
Happy的楠姐
Happy的楠姐 2020-11-30 19:08

I have a collection of MyClass that I\'d like to query using LINQ to get distinct values, and get back a Dictionary as the result, but I can\'t figure out

相关标签:
2条回答
  • 2020-11-30 19:18

    Use the ToDictionary method directly.

    var result = 
      // as Jon Skeet pointed out, OrderBy is useless here, I just leave it 
      // show how to use OrderBy in a LINQ query
      myClassCollection.OrderBy(mc => mc.SomePropToSortOn)
                       .ToDictionary(mc => mc.KeyProp.ToString(), 
                                     mc => mc.ValueProp.ToString(), 
                                     StringComparer.OrdinalIgnoreCase);
    
    0 讨论(0)
  • 2020-11-30 19:32

    Look at the ToLookup and/or ToDictionary extension methods.

    0 讨论(0)
提交回复
热议问题