我想在将列表映射到另一个列表时使用参数,我在单模式映射中找到了解决方案,但是在列表模式下它不起作用。
单模式映射解决方案:
mapper.Map<Question, AdminQuestionModel>(question,
opt => opt.AfterMap((src, dest) => dest.ImageUrl = parameter + src.Guid + ".jpg"));
我希望在这种模式下使用类似的解决方案:
mapper.Map<List<Question>, List<AdminQuestionModel>>(question,
opt => opt.AfterMap((src, dest) => dest.ImageUrl = baseImageUrl + src.Guid + ".jpg"));
在此模式下,AfterMap
参数,src
和dest
,它们并不指向模型。
更新:我无法使用Custom Value Resolvers,因为它不适用于ProjectTo
方法。
我找到了这个简单的解决方案:
mapper.Map<List<Question>, List<AdminQuestionModel>>(question,
opt => opt.AfterMap((src, dest) =>
{
int i = 0;
while (i < dest.Count)
{
dest[i].ImageUrl = baseImageUrl + src[i].Guid + ".jpg";
i++;
}
}));