Mapping multiple arrays to one type, each array contains a property

14 views
Skip to first unread message

luc.r...@gmail.com

unread,
May 26, 2016, 2:53:19 AM5/26/16
to AutoMapper-users
Hi,
I receive data oddly formatted and would like to transform it to a POCO.
The incoming data is formated as follow

string [] PROPERTY1;
string [] PROPERTY2;
int NBOCCURENCE;


The way it is currently transformed :

IEnumerable PocoList;
for(int i=0;i<NBOCCURENCE;i++)
{
    PocoList.Add(new Poco{
        Property1 = PROPERTY1[i],
        Property2 = PROPERTY2[i],
   });
}


Could you help me translate this into a pure Automapper syntax ?

Cheers
Luc

Jimmy Bogard

unread,
May 26, 2016, 11:24:48 AM5/26/16
to automapper-users
CreateMap<OddData, IEnumerable<Poco>>()
   .ConvertUsing(src => {

var PocoList = new List<Poco>();
for(int i=0;i<src.NBOCCURENCE;i++)
{
    PocoList.Add(new Poco{
        Property1 = src.PROPERTY1[i],
        Property2 = src.PROPERTY2[i],
   });
}

return PocoList;
});


var pocoList = Mapper.Map<OddData, IEnumerable<Poco>>(oddData);

There you go. Remember, AutoMapper is about mapping things that look the same. When things don't look the same, you do this. Which I'm not sure is an improvement at all. It just adds indirection (unless this is part of a broader mapping configuration).

--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages