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

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

iis - ASP.Net Core CreatedAtAction in HttpPost action returns 201 but entire request ends with 500 -