Automapper 6 : Mapping c# Func

796 views
Skip to first unread message

Jagat Ojha

unread,
Jun 6, 2017, 1:40:37 PM6/6/17
to AutoMapper-users

I have a parameter on method

 Func<UOMViewModel, dynamic> orderBy

and which I need to change to
 Func<UOM, dynamic> orderByToRepo

. After doing some research I tried following code
var config1 = new MapperConfiguration(cfg => { cfg.CreateMap<Func<UOMViewModel, dynamic>, Func<UOM, dynamic>>(); });
IMapper mapper1 = config1.CreateMapper();
var orderByToRepo= mapper1.Map<Func<UOMViewModel, dynamic>, Func<UOM, dynamic>>(orderBy);
IEnumerable<UOM> uoms = _uomRepository.GetAll(orderByToRepo, pageSize, pageNumber, sortOrder);

But while running the code I am getting error on line var orderByToRepo= mapper1.Map<Func<UOMViewModel, dynamic>, Func<UOM, dynamic>>(orderBy);

The error is

System.Func`2[Entities.Entities.UOM,System.Object] needs to have a constructor with 0 args or only optional args Parameter name: type

Is there anything I have missed. Please need help.


Thank you.

Message has been deleted

Jimmy Bogard

unread,
Jun 7, 2017, 6:02:19 AM6/7/17
to automapp...@googlegroups.com
You can't map functions. That's literally impossible, it would be rewriting IL. Have you instead considered expressions?

On Wed, Jun 7, 2017 at 10:34 AM Jagat Ojha <link2...@gmail.com> wrote:
Hi Automapper Team,

Could you help me on this. Is there any other way to implement mapping logic.

Waiting for your response.

Thank you.
--
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.
Message has been deleted

Jagat Ojha

unread,
Jun 7, 2017, 6:04:09 AM6/7/17
to AutoMapper-users
Is there any other way to map above scenario.

Waiting for response.


Thank you.

On Tuesday, June 6, 2017 at 11:25:37 PM UTC+5:45, Jagat Ojha wrote:

Jimmy Bogard

unread,
Jun 7, 2017, 6:05:18 AM6/7/17
to automapp...@googlegroups.com
You could use ConvertUsing and provide the mapping function yourself.

--
Reply all
Reply to author
Forward
0 new messages