0

sどうすれば次のようなことができますか?

.ForMember(dest => dest.Ad, opt => opt.MapFrom(src => src.Ask_Id == null ? null : new Ask { Id = src.Ask_Id }))

サポートされていないマッピングエラーが発生します。

ありがとう。

4

1 に答える 1

3

クラスオブジェクトがどのように見えるかわかりません。以下のようなクラスがあると仮定すると、

class Ask
{
    public int Id { get; set; }
}

class DestinationDto
{
    public Ask Ad { get; set; }
}

class SourceDto
{
    public int? Ask_Id { get; set; }
}

その場合は、以下のマッパーを使用してください。

Mapper.CreateMap<SourceDto, DestinationDto>()
 .ForMember(dest => dest.Ad, opt => opt.MapFrom(src => src.Ask_Id == null ? null : new Ask { Id = src.Ask_Id.Value }));

var source = new SourceDto { Ask_Id = 1}; // try with null
var destination = Mapper.Map<SourceDto, DestinationDto>(source);
于 2013-03-14T19:36:41.710 に答える