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

CanOmitCast() private static method

Determines if an instance of fromType can be assigned to an instance of toType using CLR semantics. in case of primitive type, it must rely on identity since unboxing primitive requires exact match. for nominal types, rely on subtyping.
private static CanOmitCast ( TypeUsage fromType, TypeUsage toType, bool preserveCastForDateTime ) : bool
fromType TypeUsage
toType TypeUsage
preserveCastForDateTime bool
return bool
        private static bool CanOmitCast(TypeUsage fromType, TypeUsage toType, bool preserveCastForDateTime)
        {
            var isPrimitiveType = TypeSemantics.IsPrimitiveType(fromType);

            //SQLBUDT #573573: This is to allow for a workaround on Katmai via explicit casting by the user.
            // The issue is that SqlServer's type Date maps to Edm.DateTime, same as SqlServer's DateTime and SmallDateTime.
            // However the conversion is not possible for all values of Date.

            //Note: we could also call here TypeSemantics.IsPrimitiveType(TypeUsage type, PrimitiveTypeKind primitiveTypeKind),
            //  but that checks again whether the type is primitive
            if (isPrimitiveType
                && preserveCastForDateTime
                && ((PrimitiveType)fromType.EdmType).PrimitiveTypeKind == PrimitiveTypeKind.DateTime)
            {
                return false;
            }

            if (TypeUsageEquals(fromType, toType))
            {
                return true;
            }

            if (isPrimitiveType)
            {
                return fromType.EdmType.EdmEquals(toType.EdmType);
            }

            return TypeSemantics.IsSubTypeOf(fromType, toType);
        }