System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.ValidateAndAdjustCastTypes C# (CSharp) Method

ValidateAndAdjustCastTypes() private static method

Check that the given cast specification is supported and if necessary adjust target type (for instance add precision and scale for Integral -> Decimal casts)
private static ValidateAndAdjustCastTypes ( TypeUsage toType, TypeUsage fromType, Type toClrType, Type fromClrType ) : TypeUsage
toType TypeUsage
fromType TypeUsage
toClrType Type
fromClrType Type
return TypeUsage
        private static TypeUsage ValidateAndAdjustCastTypes(TypeUsage toType, TypeUsage fromType, Type toClrType, Type fromClrType)
        {
            // only support primitives if real casting is involved
            if (toType == null
                || !TypeSemantics.IsScalarType(toType)
                || !TypeSemantics.IsScalarType(fromType))
            {
                throw new NotSupportedException(Strings.ELinq_UnsupportedCast(DescribeClrType(fromClrType), DescribeClrType(toClrType)));
            }

            var fromTypeKind = Helper.AsPrimitive(fromType.EdmType).PrimitiveTypeKind;
            var toTypeKind = Helper.AsPrimitive(toType.EdmType).PrimitiveTypeKind;

            if (toTypeKind == PrimitiveTypeKind.Decimal)
            {
                // Can't figure out the right precision and scale for decimal, so only accept integer types
                switch (fromTypeKind)
                {
                    case PrimitiveTypeKind.Byte:
                    case PrimitiveTypeKind.Int16:
                    case PrimitiveTypeKind.Int32:
                    case PrimitiveTypeKind.Int64:
                    case PrimitiveTypeKind.SByte:
                        // adjust precision and scale to ensure sufficient width
                        toType = TypeUsage.CreateDecimalTypeUsage((PrimitiveType)toType.EdmType, 19, 0);
                        break;
                    default:
                        throw new NotSupportedException(Strings.ELinq_UnsupportedCastToDecimal);
                }
            }

            return toType;
        }