我想检索cpu_ids的数组,但它会抛出
'System.Collections.Generic.List`1System.Object‘'System.Array’System.InvalidCastException。

我的代码
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);
}
}
}
}
}
}发布于 2021-05-21 09:19:21
这就是问题所在:
foreach (var stoixeio in (Array)item.Value)您正在将item.Value转换为数组,但它不是数组。这是一份名单。根据文档,Firestore数组的默认类型是List<object>。因此,只需将其转换为List<object>而不是Array (甚至只是转换为非一般的IEnumerable类型),它应该会很好。
https://stackoverflow.com/questions/67624436
复制相似问题