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

假如想象 提交于 2019-11-30 11:09:10

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<Foo>().ToDictionary(o => o.Description, o => o.Id);

If you have access to Linq, you can use the ToDictionary function.

noocyte
theList.ForEach(delegate(theObject obj) { dic.Add(obj.Id, obj.Description); });
Joel Coehoorn

Also look at the System.Collections.ObjectModel.KeyedCollection<TKey, TItem>. It seems like a better match for what you want to do.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!