Пример #1
0
        public virtual int CompareTo(BsonValue other)
        {
            // first, test if types are diferentes
            if (Type != other.Type)
            {
                // if both values are number, convert them to Double to compare
                if (IsNumber && other.IsNumber)
                {
                    return(Convert.ToDouble(RawValue).CompareTo(Convert.ToDouble(RawValue)));
                }
                // if not, order by sort type order
                return(Type.CompareTo(other.Type));
            }

            // for both values with same datatype just compare
            switch (Type)
            {
            case BsonType.Null:
            case BsonType.MinValue:
            case BsonType.MaxValue:
                return(0);

            case BsonType.Int32:
                return(((int)RawValue).CompareTo((int)other.RawValue));

            case BsonType.Int64:
                return(((long)RawValue).CompareTo((long)other.RawValue));

            case BsonType.Double:
                return(((double)RawValue).CompareTo((double)other.RawValue));

            case BsonType.String:
                return(string.Compare((string)RawValue, (string)other.RawValue));

            case BsonType.Document:
                return(AsDocument.CompareTo(other));

            case BsonType.Array:
                return(AsArray.CompareTo(other));

            case BsonType.Binary:
                return(((byte[])RawValue).BinaryCompareTo((byte[])other.RawValue));

            case BsonType.ObjectId:
                return(((ObjectId)RawValue).CompareTo((ObjectId)other.RawValue));

            case BsonType.Guid:
                return(((Guid)RawValue).CompareTo((Guid)other.RawValue));

            case BsonType.Boolean:
                return(((bool)RawValue).CompareTo((bool)other.RawValue));

            case BsonType.DateTime:
                return(((DateTime)RawValue).CompareTo((DateTime)other.RawValue));

            default:
                throw new NotImplementedException();
            }
        }
Пример #2
0
        public virtual int CompareTo(BsonValue other)
        {
            // first, test if types are different
            if (Type != other.Type)
            {
                // if both values are number, convert them to Decimal (128 bits) to compare
                // it's the slowest way, but more secure
                if (IsNumber && other.IsNumber)
                {
                    return(Convert.ToDecimal(RawValue).CompareTo(Convert.ToDecimal(other.RawValue)));
                }
                // if not, order by sort type order
                else
                {
                    return(Type.CompareTo(other.Type));
                }
            }

            // for both values with same data type just compare
            switch (Type)
            {
            case BsonType.Null:
            case BsonType.MinValue:
            case BsonType.MaxValue:
                return(0);

            case BsonType.Int32: return(((Int32)RawValue).CompareTo((Int32)other.RawValue));

            case BsonType.Int64: return(((Int64)RawValue).CompareTo((Int64)other.RawValue));

            case BsonType.Double: return(((Double)RawValue).CompareTo((Double)other.RawValue));

            case BsonType.Decimal: return(((Decimal)RawValue).CompareTo((Decimal)other.RawValue));

            case BsonType.String: return(string.Compare((String)RawValue, (String)other.RawValue));

            case BsonType.Document: return(AsDocument.CompareTo(other));

            case BsonType.Array: return(AsArray.CompareTo(other));

            case BsonType.Binary: return(((Byte[])RawValue).BinaryCompareTo((Byte[])other.RawValue));

            case BsonType.ObjectId: return(((ObjectId)RawValue).CompareTo((ObjectId)other.RawValue));

            case BsonType.Guid: return(((Guid)RawValue).CompareTo((Guid)other.RawValue));

            case BsonType.Boolean: return(((Boolean)RawValue).CompareTo((Boolean)other.RawValue));

            case BsonType.DateTime:
                var d0 = (DateTime)RawValue;
                var d1 = (DateTime)other.RawValue;
                if (d0.Kind != DateTimeKind.Utc)
                {
                    d0 = d0.ToUniversalTime();
                }
                if (d1.Kind != DateTimeKind.Utc)
                {
                    d1 = d1.ToUniversalTime();
                }
                return(d0.CompareTo(d1));

            default: throw new NotImplementedException();
            }
        }