You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在MAUI中通过Plugin.Firebase.Firestore将IDocumentSnapshot转为List<T>

解决方法

当然有更简洁的方式,直接用LINQ的Select结合ToList()就能一步完成转换,不需要手动写foreach循环:

using System.Linq; // 需要引入这个命名空间

// ...

var query = await CrossFirebaseFirestore
    .Current
    .GetCollection("NewCollection")
    .GetDocumentsAsync<Pokemon>();

List<Pokemon> allData = query.Documents.Select(doc => doc.Data).ToList();

补充说明

  • IDocumentSnapshot<Pokemon>的Data属性本身就是Pokemon类型,通过Select可以把每个快照映射为对应的实体对象,再用ToList()直接生成List<Pokemon>。
  • 这种方式和手动foreach的逻辑本质一致,但代码更简洁优雅,是C#中集合转换的常规写法。

内容的提问来源于stack exchange,提问作者Reza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 09:23:39