c# - Mapping List<Model> to Dictionary<int, ViewModel> -


i have model class:

public class model {     public int id {get;set;}     public string name {get;set;} } 

and view model:

public class viewmodel {     public string name {get;set;} } 

i want map list dictionary key model.id.

i have started such configuration:

configuration     .createmap<model, keyvaluepair<int, viewmodel>>()     .constructusing(         x =>             new keyvaluepair<int, viewmodel>(x.id, _mapper.map<viewmodel>(x))); 

but don't want use mapper instance in configuration. there other way achieve this? i've seen answers, people used x.mapto(), doesn't seem available anymore...

you can use mapper instance lambda parameter x.engine.mapper

simple this

configuration     .createmap<model, keyvaluepair<int, viewmodel>>()     .constructusing(context => new keyvaluepair<int, viewmodel>(         ((model)context.sourcevalue).id,         context.engine.mapper.map<viewmodel>(context.sourcevalue))); 

Comments

Popular posts from this blog

ssh - Vagrant Windows - ssh_exchange_identification: read: Connection reset by peer -

gcc - Neither ld wrap nor LD_PRELOAD working to intercept system call -

maven - jacoco coverage per test setup -