AutoMapper ignore properties while mapping a list

Is it possible in AutoMapper to ignore certain properties while mapping a list? For example, I have two classes Metadata and MetadataInput.

Both have the same fields except for destination, "MetadataInput", which has an extra field.

Mapper.CreateMap <IList<Metadata>, IList<MetadataInput>>()

I've tried to use the formember option but it generates errors, probably because the property cannot be used when you want to map a list? If yes, is there alternative solution?


As Darin Dimitrov suggested, you should not try and map lists or collections.

If you have a 1 -> 1 relationship between them all, just make a map like:

Mapper.CreateMap<Metadata, MetadataInput>().ForMember(s => s.Property, t => t.Ignore());

Then you could use your list and select it to the other list.

var metadataList = new List<Metadata>();

var meatadataInputList = metadataList.Select(p => Mapper.Map<MetadataInput>(p).ToList();

Thanks for the useful comments. Because both lists are already made before mapping I've done it this way:

Gets the list from the db:

List<Metadata> metadatas = _Metadataservice.GetList(_Collectionservice.Get("Koekelare").ID).ToList();

Create the mapper (thanks for pointing out my mistake):

Mapper.CreateMap<Metadata, MetadataInput>().ForMember(s => s.Property, t => t.Ignore());

Make a new list and map the new (single) values one by one:

List<MetadataInput> meta = new List<MetadataInput>();
for (int i = 0; i < e.Count; i++)
{
  meta.Add(Mapper.Map<Metadata, MetadataInput>(metadatas[i], input.Metadatas[i]));
}

Could someone confirm this is a good way?


使用这个映射器配置来获得所需的结果:

Mapper.CreateMap<Output, Input>().ForMember(s => s.InputProperty1, t => t.Ignore());
Mapper.CreateMap<Output, Input>().ForMember(s => s.InputProperty2, t => t.Ignore());
Mapper.CreateMap<Input, Output>();
listOfItems = Mapper.Map<List<Input>, List<Output>>(InputListObject);
链接地址: http://www.djcxy.com/p/37372.html

上一篇: AutoMapper将对象映射到MS Dynamics CRM中的查找字段

下一篇: AutoMapper在映射列表时忽略属性