简体   繁体   中英

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'". It only works for a single BsonDocument Type.

How do I Deserialize a BsonDocument list to list class type?

        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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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