Skip to main content
The 2024 Developer Survey results are live! See the results

I encounter a problem when I try to map DateTime?DateTime? to stringstring, if source value is null,it it will not step in extension method, do anyone know why? and I try automapper 10.0 it's normal!

version Automapper

Automapper 11.0   

.Net6

below is my source code

void Main()
{
    var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Source, Destination>()
               .ForMember(dest => dest.Date, 
                  opt =>opt.MapFrom(src => src.Date.ToCommon()/));
               
        });

    IMapper mapper = config.CreateMapper();

    var source = new Source { Date = null };
    var destination = mapper.Map<Destination>(source);

    Console.WriteLine(destination.Date);
}


public static class Temp
{
    public static string ToCommon(this DateTime? dateTime)
    {
        if (dateTime is null)
            return "something";

        return dateTime.Value.ToString("yyyy/MM/dd");
    }
}

public class Source
{
    public DateTime? Date { get; set; }
}

public class Destination
{
    public string Date { get; set; }
}

I expect result should be return "something" ,but return null

I encounter a problem when I try to map DateTime? to string, if source value is null,it will not step in extension method, do anyone know why? and I try automapper 10.0 it's normal!

version Automapper 11.0  .Net6

below is my source code

void Main()
{
    var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Source, Destination>()
               .ForMember(dest => dest.Date, opt =>opt.MapFrom(src => src.Date.ToCommon()/));
               
        });

    IMapper mapper = config.CreateMapper();

    var source = new Source { Date = null };
    var destination = mapper.Map<Destination>(source);

    Console.WriteLine(destination.Date);
}


public static class Temp
{
    public static string ToCommon(this DateTime? dateTime)
    {
        if (dateTime is null)
            return "something";

        return dateTime.Value.ToString("yyyy/MM/dd");
    }
}

public class Source
{
    public DateTime? Date { get; set; }
}

public class Destination
{
    public string Date { get; set; }
}

I expect result should be return "something" ,but return null

I encounter a problem when I try to map DateTime? to string, if source value is null, it will not step in extension method, do anyone know why? and I try automapper 10.0 it's normal!

version

Automapper 11.0 

.Net6

below is my source code

void Main()
{
    var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Source, Destination>()
               .ForMember(dest => dest.Date, 
                  opt =>opt.MapFrom(src => src.Date.ToCommon()));
               
        });

    IMapper mapper = config.CreateMapper();

    var source = new Source { Date = null };
    var destination = mapper.Map<Destination>(source);

    Console.WriteLine(destination.Date);
}


public static class Temp
{
    public static string ToCommon(this DateTime? dateTime)
    {
        if (dateTime is null)
            return "something";

        return dateTime.Value.ToString("yyyy/MM/dd");
    }
}

public class Source
{
    public DateTime? Date { get; set; }
}

public class Destination
{
    public string Date { get; set; }
}

I expect result should be return "something" ,but return null

Source Link

nullable datetime cannot map through extension method

I encounter a problem when I try to map DateTime? to string, if source value is null,it will not step in extension method, do anyone know why? and I try automapper 10.0 it's normal!

version Automapper 11.0 .Net6

below is my source code

void Main()
{
    var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Source, Destination>()
               .ForMember(dest => dest.Date, opt =>opt.MapFrom(src => src.Date.ToCommon()/));
               
        });

    IMapper mapper = config.CreateMapper();

    var source = new Source { Date = null };
    var destination = mapper.Map<Destination>(source);

    Console.WriteLine(destination.Date);
}


public static class Temp
{
    public static string ToCommon(this DateTime? dateTime)
    {
        if (dateTime is null)
            return "something";

        return dateTime.Value.ToString("yyyy/MM/dd");
    }
}

public class Source
{
    public DateTime? Date { get; set; }
}

public class Destination
{
    public string Date { get; set; }
}

I expect result should be return "something" ,but return null

created from staging ground