AutoMapper flatten nested collections

十年热恋 提交于 2019-12-01 18:44:35

Because the cardinality of the root objects isn't 1:1, (i.e. 2 root Merchants need to map to 6 OrderViewModels), you may need to resort to a custom TypeConverter and operate at the collection level, where you can use .SelectMany to do the flattening:

public class MyTypeConverter : ITypeConverter<IEnumerable<Merchant>, List<OrderViewModel>>
{
    public List<OrderViewModel> Convert(ResolutionContext context)
    {
        if (context == null || context.IsSourceValueNull)
            return null;

        var source = context.SourceValue as IEnumerable<Merchant>;

        return source
            .SelectMany(s => s.Orders
              .Select(o => new OrderViewModel
              {
                  MerchantName = s.MerchantName,
                  OrderId = o.OrderId
              }))
              .ToList();
    }
}

Which you can then bootstrap:

Mapper.CreateMap<IEnumerable<Merchant>, List<OrderViewModel>>()
    .ConvertUsing<MyTypeConverter>();

And then mapped as such:

var models = Mapper.Map<List<OrderViewModel>>(myMerchants);

An interesting finding is that, only do the below is enough to achieve the goal without automapper.

var models = myMerchants.SelectMany(s => s.Orders.Select(o => new OrderViewModel { MerchantName = s.MerchantName, OrderId = o.OrderId })).ToList();

Old question but thought would be helpful for newer versions.

I am using .Net core 2 with automapper. I prefer to do the ProjectTo extension of queryable

queryable
   .SelectMany(outterClass => outterClass.innerList)
   .AsQueryable()
   .ProjectTo<OutterClassDto>();

Then, configure like so:

config.CreateMap<OuterClass, OuterClassDto>();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!