将哈希表元素转换为列表

本文关键字:列表 转换 元素 哈希表 | 更新日期: 2023-09-27 18:09:58

我有这个

哈希表有一个

key "ids" and its values are [1,2,3]
List<long> ids= (List<long>)hashtable["ids"];

在尝试强制转换时发生错误。上面写着

Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'System.Collections.Generic.List`1[System.Int64]'.
我已经被困了好几个小时了,有什么建议吗?

将哈希表元素转换为列表

如果你在你的问题中写下你期望得到的值和你的哈希表的定义,那将是有帮助的。
假设您正在尝试获得[1,2,3]和您的"值"是long的数组,尝试:

List<long> ids= hashtable["ids"].ToList();
    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;
    }