Let’s say I have ResponseModels<T>
and ResponseDTOs<T>
both with properties IEnumerable<T>
and int TotalCount
I know I can create generic mapping using
CreateMap(typeof(ResponseModels<>), typeof(ResponseDTOs<>));
My goal is to take only MaxOutListLength
of items during mapping. I know I can create complex mapping for member using the following (but only if generic type is defined).
CreateMap<ResponseModels<Order>, ResponseDTOs<OrderDTO>>()
.ForMember(
dst => dst.Items,
opt => opt.MapFrom(
src =>
src.Items!.Take(MaxOutListLength))
)
.ForMember(
dst => dst.Count,
opt => opt.MapFrom(
src =>
src.Items.Count() > MaxOutListLength ? MaxOutListLength : src.Items.Count())
);
How can I combine these two approaches?
I tried the following code, but object does not contain a definition for 'Items'
CreateMap(typeof(ResponseModels<>), typeof(ResponseDTOs<>))
.ForMember(
dst => dst.Items,
opt => opt.MapFrom(
src =>
src.Items!.Take(MaxOutListLength))
)
.ForMember(
dst => dst.Count,
opt => opt.MapFrom(
src =>
src.Items.Count() > MaxOutListLength ? MaxOutListLength : src.Items.Count())
);
2