简体   繁体   English

C# Mongodb 将 BsonDocument 类型列表反序列化为 Class 类型列表

[英]C# Mongodb Deserialize List of BsonDocument types to List of Class Types

I am trying to deserialize a List of BsonDocuments to class Type using BsonSerializer but I am getting an error "cannot convert from 'System.Collections.Generic.List<MongoDB.Bson.BsonDocument>' to 'MongoDB.Bson.BsonDocument'".我正在尝试使用 BsonSerializer 将 BsonDocuments 列表反序列化为 class 类型,但出现错误“无法从 'System.Collections.Generic.List<MongoDB.Bson.BsonDocument>' 转换为 'MongoDB.Bson.BsonDocument'”。 It only works for a single BsonDocument Type.它仅适用于单个 BsonDocument 类型。

How do I Deserialize a BsonDocument list to list class type?如何反序列化 BsonDocument 列表以列出 class 类型?

        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);

I had the same problem, a dumb way to achieve it via foreach .我有同样的问题,通过foreach实现它的愚蠢方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM