使用 AutoMapper 映射两个不同类型的集合
Map two Collection of different type using AutoMapper
我有一个 DTO 和一个实体,两者都称为 QuestionnaireSubmission
映射器,如下所示:
public QuestionSubmissionMapper() => this.CreateMap<DTO.QuestionnaireSubmission, QuestionnaireSubmission>()
.ForMember(d => d.ParticipantId, d => d.MapFrom(s => s.ParticipationId))
.ForMember(d => d.SurveyVersionId, d => d.MapFrom(s => s.Survey.Version))
.ForMember(d => d.RespondantAnswers, d => d.MapFrom(s => s.States));
我的问题是我不知道如何映射 s.States of type State[]
和 RespondantAnswers of type IEnumerable<RespondantAnswer>
我已经在这两个之间创建了一个映射 类 来映射它们。
this.CreateMap<State, RespondantAnswer>()
.ForMember(d => d.QuestionId, d => d.MapFrom(s => s.Subject))
.ForMember(..)
.ForMember(..);
但是 Automapper 似乎无法进行映射...我错过了什么?我真的必须使用 .ConvertUsing()
函数吗?
我无法真正重现该问题,但以下内容似乎按预期工作。 AutoMapper 应该能够在没有特殊设置的情况下处理集合:
var mapper = new MapperConfiguration(cfg => {
cfg.CreateMap<SourceQuestionnaireSubmission, DestQuestionnaireSubmission>()
.ForMember(d => d.ParticipantId, d => d.MapFrom(s => s.ParticipationId))
.ForMember(d => d.SurveyVersionId, d => d.MapFrom(s => s.Survey.Version))
.ForMember(d => d.RespondantAnswers, d => d.MapFrom(s => s.States));
cfg.CreateMap<State, RespondantAnswer>()
.ForMember(d => d.QuestionId, d => d.MapFrom(s => s.QuestionId));
})
.CreateMapper();
var source = new SourceQuestionnaireSubmission
{
ParticipationId = 1,
Survey = new Survey { Version = 10 },
States = new []
{
new State { QuestionId = 100 },
new State { QuestionId = 999 }
}
};
var dest = mapper.Map<SourceQuestionnaireSubmission, DestQuestionnaireSubmission>(source);
Console.WriteLine("ParticipantId: {0}", dest.ParticipantId);
Console.WriteLine("SurveyVersionId: {0}", dest.SurveyVersionId);
Console.WriteLine("RespondantAnswers: {0}",
string.Join(", ", dest.RespondantAnswers.Select(a => a.QuestionId)));
// Output:
// ParticipantId: 1
// SurveyVersionId: 10
// RespondantAnswers: 100, 999
请参阅 this fiddle 进行演示。
我有一个 DTO 和一个实体,两者都称为 QuestionnaireSubmission
映射器,如下所示:
public QuestionSubmissionMapper() => this.CreateMap<DTO.QuestionnaireSubmission, QuestionnaireSubmission>()
.ForMember(d => d.ParticipantId, d => d.MapFrom(s => s.ParticipationId))
.ForMember(d => d.SurveyVersionId, d => d.MapFrom(s => s.Survey.Version))
.ForMember(d => d.RespondantAnswers, d => d.MapFrom(s => s.States));
我的问题是我不知道如何映射 s.States of type State[]
和 RespondantAnswers of type IEnumerable<RespondantAnswer>
我已经在这两个之间创建了一个映射 类 来映射它们。
this.CreateMap<State, RespondantAnswer>()
.ForMember(d => d.QuestionId, d => d.MapFrom(s => s.Subject))
.ForMember(..)
.ForMember(..);
但是 Automapper 似乎无法进行映射...我错过了什么?我真的必须使用 .ConvertUsing()
函数吗?
我无法真正重现该问题,但以下内容似乎按预期工作。 AutoMapper 应该能够在没有特殊设置的情况下处理集合:
var mapper = new MapperConfiguration(cfg => {
cfg.CreateMap<SourceQuestionnaireSubmission, DestQuestionnaireSubmission>()
.ForMember(d => d.ParticipantId, d => d.MapFrom(s => s.ParticipationId))
.ForMember(d => d.SurveyVersionId, d => d.MapFrom(s => s.Survey.Version))
.ForMember(d => d.RespondantAnswers, d => d.MapFrom(s => s.States));
cfg.CreateMap<State, RespondantAnswer>()
.ForMember(d => d.QuestionId, d => d.MapFrom(s => s.QuestionId));
})
.CreateMapper();
var source = new SourceQuestionnaireSubmission
{
ParticipationId = 1,
Survey = new Survey { Version = 10 },
States = new []
{
new State { QuestionId = 100 },
new State { QuestionId = 999 }
}
};
var dest = mapper.Map<SourceQuestionnaireSubmission, DestQuestionnaireSubmission>(source);
Console.WriteLine("ParticipantId: {0}", dest.ParticipantId);
Console.WriteLine("SurveyVersionId: {0}", dest.SurveyVersionId);
Console.WriteLine("RespondantAnswers: {0}",
string.Join(", ", dest.RespondantAnswers.Select(a => a.QuestionId)));
// Output:
// ParticipantId: 1
// SurveyVersionId: 10
// RespondantAnswers: 100, 999
请参阅 this fiddle 进行演示。