I am receiving an IEnumerable<Class1> from a third party api.
I would like to map it using Automapper to the following class:
public class WrapperClass
{
public List<Class2> Items { get; set; }
}
So essentially I am thinking I need two mappings:
IEnumerable<Class1> ----> WrapperClass
_______
Class1 ----> Class2
How would I approach this in Automapper ?
Yes, you must first create mappings for map Class1 to Class2 like:
AutoMapper.Mapper.CreateMap<Class1, Class2>().ForMember(x => x.A, y => y.MapFrom(z => z.B));
Then create mapping for map collection on class like:
AutoMapper.Mapper.CreateMap<IEnumerable<Class1>, WrapperClass>().ForMember(x => x.Items, y => y.MapFrom(z => z));
Finally you can use it like:
var wrapperClass = AutoMapper.Mapper.Map<IEnumerable<Class1>, WrapperClass>(/* list */);
Below three class used for this example:
public class WrapperClass
{
public List<Class2> Items { get; set; }
}
public class Class2 {
public int A { get; set; }
}
public class Class1
{
public int B { get; set; }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.