在自动映射器中使用参数映射列表

问题描述 投票:0回答:1

我想在将列表映射到另一个列表时使用参数,我在单模式映射中找到了解决方案,但是在列表模式下它不起作用。

单模式映射解决方案:

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参数,srcdest,它们并不指向模型。

更新:我无法使用Custom Value Resolvers,因为它不适用于ProjectTo方法。

asp.net-core automapper ef-core-2.0
1个回答
0
投票

我找到了这个简单的解决方案:

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++;
                }
            }));
© www.soinside.com 2019 - 2024. All rights reserved.