private static Expression GetParseDateTimeToStringOrNull(Expression dateValue, Type sqlFunctionsType)
        {
            if (!TryGetDatePartMethod <DateTime?>(sqlFunctionsType, out var datePartMethod))
            {
                return(null);
            }

            dateValue = dateValue.GetConversionTo <DateTime?>();

            var dateTimePattern = CultureInfo
                                  .CurrentCulture
                                  .DateTimeFormat
                                  .SortableDateTimePattern
                                  .Replace("mm", "mi") // Minutes
                                  .Replace('T', ' ')   // Date-Time separator
                                  .Replace("'", null)
                                  .ToLowerInvariant();

            Expression valueConcatenation = null;
            var        datePartStartIndex = 0;
            var        stringConcat       = StringExpressionExtensions.GetConcatMethod(parameterCount: 2);

            for (var i = 0; i < dateTimePattern.Length; ++i)
            {
                if (char.IsLetter(dateTimePattern[i]))
                {
                    continue;
                }

                var datePartNameCall = GetDatePartCall(
                    datePartMethod,
                    dateTimePattern,
                    datePartStartIndex,
                    i,
                    dateValue,
                    sqlFunctionsType);

                var added = Expression.Call(
                    stringConcat,
                    datePartNameCall,
                    dateTimePattern[i].ToString().ToConstantExpression());

                valueConcatenation = (valueConcatenation != null)
                    ? Expression.Call(stringConcat, valueConcatenation, added) : added;

                datePartStartIndex = i + 1;
            }

            var finalDatePartNameCall = GetDatePartCall(
                datePartMethod,
                dateTimePattern,
                datePartStartIndex,
                dateTimePattern.Length,
                dateValue,
                sqlFunctionsType);

            // ReSharper disable once AssignNullToNotNullAttribute
            return(Expression.Call(stringConcat, valueConcatenation, finalDatePartNameCall));
        }
Exemple #2
0
        private static Expression GetStringValueConversion(
            Expression sourceValue,
            Expression fallbackValue,
            Type nonNullableTargetEnumType)
        {
            if (sourceValue.Type != typeof(string))
            {
                sourceValue = ToStringConverter.GetConversion(sourceValue);
            }

            var underlyingEnumType = Enum.GetUnderlyingType(nonNullableTargetEnumType);

            var isNumericTest = GetIsNumericTest(sourceValue);

            var numericConversion = GetNumericStringToEnumConversion(
                sourceValue,
                fallbackValue,
                nonNullableTargetEnumType,
                underlyingEnumType);

            var nameMatchingConversion = GetStringToEnumConversion(
                sourceValue,
                fallbackValue,
                nonNullableTargetEnumType);

            var numericOrNameConversion = Expression.Condition(
                isNumericTest,
                numericConversion,
                nameMatchingConversion);

            var convertedValueOrDefault = Expression.Condition(
                StringExpressionExtensions.GetIsNullOrWhiteSpaceCall(sourceValue),
                fallbackValue,
                numericOrNameConversion);

            return(convertedValueOrDefault);
        }