user3770093
user3770093

Reputation: 345

Convert Hashtable elements to List

I have this

hashtable has a

key "ids" and its values are [1,2,3]

List<long> ids= (List<long>)hashtable["ids"];

an error occurs when trying to cast. It says

Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'System.Collections.Generic.List`1[System.Int64]'.

I've been stuck for hours, any suggestions?

Upvotes: 0

Views: 8147

Answers (2)

user6800884
user6800884

Reputation: 11

    public static List<dynamic> ToDynamicList(this Hashtable ht)
    {
        var result = new List<dynamic>();
        foreach (DictionaryEntry de in ht)
        {
            result.Add(new { key = de.Key, value = de.Value });
        }
        return result;
    }

Upvotes: 1

Avi Turner
Avi Turner

Reputation: 10466

It would be helpful if you wrote in your question what are the values you expect to get and the definition of your hashtable.
Assuming You are trying to get [1,2,3] and your 'Value' is an array of long, try:

List<long> ids= hashtable["ids"].ToList();

Upvotes: 1

Related Questions