automapper多对象的一个对象 [英] automapper multi objects to one object
问题描述
我有两个子类,我需要一个列表元素从复制到主对象
<预类=郎-CS prettyprint-覆盖>
公共类Foo1:安诺
{
公共BOOL ISON {搞定;设置;}
公开名单<安诺> a注解{搞定;私人集;}
公共Foo1()
{
this.Anotes =新的List<安诺>();
}
}
公共类foo2的:Bnote
{
公共BOOL ISON {搞定;设置;}
公开名单< Bnote> a注解{搞定;私人集;}
公共foo2的()
{
this.Anotes =新的List< Bnote>();
}
}
公共类Foo3:C注
{
公共BOOL ISON {搞定;设置;}
公开名单< C注> a注解{搞定;私人集;}
公开名单< C注> Bnotes {搞定;私人集; }
}
我将从数据库中retreiving数据,并把这个数据转换成Foo1和foo2的。那么我需要
在Foo1和foo2的清单数据映射到在Foo3 List中的元素。
我已经做了
<预类=郎-CS prettyprint-覆盖>
Foo1A foo1a =新Foo1A();
Foo2A foo2a =新Foo2A();
Mapper.CreateMap< Foo1,Foo1A>();
Mapper.CreateMap< foo2的,F002A>();
Mapper.Map< Foo1,Foo2A>(foo1,foo1a);
Mapper.Map< foo2的,F002A>(foo2的,foo2a);
和这个作品,但我需要做的就是
地图列表a注解中Foo1到列表a注解在Foo3
映射表a注解在foo2的到列表Bnotes在Foo3。
您应该只能够做到:
<预类=郎-CS prettyprint-覆盖>
Mapper.CreateMap< ; A注解,C注>();
Mapper.CreateMap< Foo1,Foo3>()
.ForMember(DEST => dest.ANotes,选择=> opt.MapFrom(SRC => src.ANotes))
.ForMember(DEST => dest.BNotes,选择=> opt.Ignore());
Mapper.CreateMap< foo2的,Foo3>()
.ForMember(DEST => dest.BNotes,选择=> opt.MapFrom(SRC => src.ANotes))
.ForMember(DEST => dest.ANotes,选择=> opt.Ignore());
Foo3 foo3 =新Foo3();
Mapper.Map< Foo1,Foo3>(富,foo3);
Mapper.Map< foo2的,Foo3>(foo2的,foo3);
foo3.ANotes
和 foo3.BNotes
都应该被正确映射。
I have two sub class that I need to copy a List element from into a master object
public Class Foo1 : Anote
{
public bool Ison { get; set;}
public List<Anote>Anotes { get; private set;}
public Foo1()
{
this.Anotes = new List<Anote>();
}
}
public Class Foo2 : Bnote
{
public bool Ison { get; set;}
public List<Bnote>Anotes { get; private set;}
public Foo2()
{
this.Anotes = new List<Bnote>();
}
}
public Class Foo3 : Cnote
{
public bool Ison { get; set;}
public List<Cnote>Anotes { get; private set;}
public List<Cnote>Bnotes { get; private set; }
}
I will be retreiving data from a database and putting this data into Foo1 and Foo2. I then need to Map the List data from Foo1 and Foo2 into the List elements in Foo3.
I have done
Foo1A foo1a = new Foo1A();
Foo2A foo2a = new Foo2A();
Mapper.CreateMap<Foo1, Foo1A>();
Mapper.CreateMap<Foo2, F002A>();
Mapper.Map<Foo1, Foo2A>(foo1, foo1a);
Mapper.Map<Foo2, F002A>(foo2, foo2a);
and this works but what I need to do is
Map the List Anotes in Foo1 to the List Anotes in Foo3 Map the List Anotes in Foo2 to the List Bnotes in Foo3.
You should just be able to do:
Mapper.CreateMap<ANote, CNote>();
Mapper.CreateMap<Foo1, Foo3>()
.ForMember(dest => dest.ANotes, opt => opt.MapFrom(src => src.ANotes))
.ForMember(dest => dest.BNotes, opt => opt.Ignore());
Mapper.CreateMap<Foo2, Foo3>()
.ForMember(dest => dest.BNotes, opt => opt.MapFrom(src => src.ANotes))
.ForMember(dest => dest.ANotes, opt => opt.Ignore());
Foo3 foo3 = new Foo3();
Mapper.Map<Foo1, Foo3>(foo, foo3);
Mapper.Map<Foo2, Foo3>(foo2, foo3);
foo3.ANotes
and foo3.BNotes
should both have been mapped correctly.
这篇关于automapper多对象的一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!