【问题标题】:C# Mongodb Deserialize List of BsonDocument types to List of Class TypesC# Mongodb 将 BsonDocument 类型列表反序列化为类类型列表
【发布时间】:2021-04-05 11:40:17
【问题描述】:

我正在尝试使用 BsonSerializer 将 BsonDocuments 列表反序列化为类 Type,但出现错误“无法从 'System.Collections.Generic.List' 转换为 'MongoDB.Bson.BsonDocument '”。它仅适用于单个 BsonDocument 类型。

如何反序列化 BsonDocument 列表以列出类类型?

        var projection = Builders<Property>.Projection
            .Include(x => x.Type);

        var values= await MongoCollection
                                .Find(Builders<Property>.Filter.Empty)
                                .Project(projection)
                                .ToListAsync()
                                .ConfigureAwait(false);

        var test = BsonSerializer.Deserialize<IEnumerable<Property>>(values);

【问题讨论】:

标签: c# mongodb mongodb-.net-driver


【解决方案1】:

我遇到了同样的问题,通过foreach 实现它的愚蠢方法。

var list = new List<Property>();
foreach (var item in values) {
    var model = BsonSerializer.Deserialize<Property>(item);
    list.Add(model);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 2022-01-02
    • 1970-01-01
    相关资源
    最近更新 更多