C#云firestore检索数组
我想检索cpu_ID数组,但它会抛出 System.InvalidCastException:'System.Collections.Generic.List'1[System.Object]'System.Array'。 我的代码C#云firestore检索数组,c#,firebase,visual-studio,google-cloud-firestore,C#,Firebase,Visual Studio,Google Cloud Firestore,我想检索cpu_ID数组,但它会抛出 System.InvalidCastException:'System.Collections.Generic.List'1[System.Object]'System.Array'。 我的代码 public static async Task RetriveDataFromFirestore(string email, string digitalkey) { Query collecti
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);
}
}
}
}
}
}
公共静态异步任务从FireStore检索数据(字符串电子邮件、字符串数字键)
{
查询集合=数据库。集合(“客户”)。文档(用户ID)。集合(“订阅”);
QuerySnapshot subscriptions=wait collection.GetSnapshotAsync();
foreach(订阅中使用的文档快照)
{
DocumentReference docref=Database.Collection(“customers”).Document(UserId).Collection(“subscriptions”).Document(use.Id.ToString());
DocumentSnapshot snap=await docref.GetSnapshotAsync();
if(snap.Exists)
{
Dictionary key=snap.ToDictionary();
foreach(键中的var项)
{
//控制台写入线(项目);
如果(item.Key==“cpu\U ID”)
{
foreach(数组中的var stoixeio item.Value)
{
控制台写入线(stoixeio);
}
}
}
}
}
}
这就是问题所在:
foreach(数组中的var stoixeio item.Value)
您正在将
item.Value
强制转换为数组,但它不是数组。这是一张单子。根据,Firestore阵列的默认类型为列表
。因此,只需转换为列表
,而不是数组
(或者甚至只转换为非泛型的IEnumerable
类型),就可以了。请发布完整的堆栈跟踪,理想情况下是一个?(所有这些问号的例外看起来有点奇怪……这些想法来自何方?)我怀疑这是否真的与Visual Studio有关,因此我建议删除该标记。