首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#云防火墙检索数组

C#云防火墙检索数组
EN

Stack Overflow用户
提问于 2021-05-20 16:28:12
回答 1查看 410关注 0票数 1

我想检索cpu_ids的数组,但它会抛出

'System.Collections.Generic.List`1System.Object‘'System.Array’System.InvalidCastException。

我的代码

代码语言:javascript
复制
    public static async Task RetriveDataFromFirestore(string email, string digitalkey)
    {    

       
            Query collection = Database.Collection("customers").Document(UserId).Collection("subscriptions");
            QuerySnapshot subscriptions = await collection.GetSnapshotAsync();

            foreach(DocumentSnapshot use in subscriptions)
            {
                DocumentReference docref = Database.Collection("customers").Document(UserId).Collection("subscriptions").Document(use.Id.ToString());
                DocumentSnapshot snap = await docref.GetSnapshotAsync();

                
                if (snap.Exists)
                {
                    Dictionary<string, object> key = snap.ToDictionary();
                    
                    foreach (var item in key)
                    {
                        //Console.WriteLine(item);
                        if (item.Key == "cpu_ids")
                        {
                            foreach (var stoixeio in (Array)item.Value)
                            {
                                Console.WriteLine(stoixeio);
                            }
                        }
                    }
                }
            }
        
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-21 09:19:21

这就是问题所在:

代码语言:javascript
复制
foreach (var stoixeio in (Array)item.Value)

您正在将item.Value转换为数组,但它不是数组。这是一份名单。根据文档,Firestore数组的默认类型是List<object>。因此,只需将其转换为List<object>而不是Array (甚至只是转换为非一般的IEnumerable类型),它应该会很好。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67624436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档