18

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 and Id as the value.

This is so the Hashtable can then be serialised to JSON.

Is there a way to convert from List<Object> to Hashtable without writing a loop to go through each item in the list?


I think I made it not clear, what I'm trying to achieve.

What I'm trying to do is, if there is an instance running, accessing this one directly, without starting the clickonce url. I searching for a solution, where I don't have to write a little program (which has to be deployed as well, ...), which checks if the app is running, if yes hand over the params, if not starting the clickonce url.

The background update is not really an option, because this "connecting to app" screen is still there and consuming time, and it's a must, that every user is running at every time the most recent version of the app.

4

4 に答える 4

32

List に Foo 型のオブジェクト (int ID と文字列 Description) が含まれているとします。

Linq を使用して、次のようにそのリストを Dictionary に変換できます。

var dict = myList.Cast<Foo>().ToDictionary(o => o.Description, o => o.Id);
于 2008-10-03T10:13:52.047 に答える
5

Linq にアクセスできる場合は、ToDictionary関数を使用できます。

于 2008-10-03T10:12:43.297 に答える
0
theList.ForEach(delegate(theObject obj) { dic.Add(obj.Id, obj.Description); });
于 2008-10-03T10:31:43.460 に答える
-1

も見てくださいSystem.Collections.ObjectModel.KeyedCollection<TKey, TItem>。自分のやりたいこととの相性が良さそうです。

于 2008-10-03T12:58:40.533 に答える