How can I convert List<object> to Hashtable in C#?

后端 未结 4 1534
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-01 16:12

I have a list of objects, each containing an Id, Code and Description.

I need to convert this list into a Hashtable, using Description as the key an

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-01 16:54

    Let's assume that your List contains objects of type Foo (with an int Id and a string Description).

    You can use Linq to turn that list into a Dictionary like this:

    var dict = myList.Cast().ToDictionary(o => o.Description, o => o.Id);
    

提交回复
热议问题