user3770093 user3770093 - 3 months ago 38
C# Question

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?

Answer

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();