The best option would probably to go with this:
monto = document["monto"].ToDecimal();
The reason why this works so nicely is that the abstract BsonValue
type implements a method that looks like this:
public virtual decimal ToDecimal()
The concrete implementations of BsonValue
(e.g. BsonInt32
, BsonInt64
, BsonDouble
etc.) override this member with the below method which gives you precisely what you want, no matter if the driver deserializes an int, a long or a double from a stored document:
public override decimal ToDecimal()
{
return (decimal)_value;
}
Alternatively, in your specific case (and if all documents in scope have an int stored in their "monto" field) you can simply write:
monto = Convert.ToDecimal(document["monto"].AsInt32)
or even just
monto = (decimal)(document["monto"].AsInt32)
which are semantically identical.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…