Automapper Kopiuj listę do listy
Mam takie klasy:
public class Person {
public int Id{ get; set ;}
public string FirstName{ get; set ;}
public string LastName{ get; set ;}
}
public class PersonView {
public int Id{ get; set ;}
public string FirstName{ get; set ;}
public string LastName{ get; set ;}
}
Zdefiniowałem to:
Mapper.CreateMap<Person, PersonView>();
Mapper.CreateMap<PersonView, Person>()
.ForMember(person => person.Id, opt => opt.Ignore());
To jest praca dla tego:
PersonView personView = Mapper.Map<Person, PersonView>(new Person());
Chciałbym zrobić to samo, ale dla List<Person>
do List<PersonView>
ale nie znajduję odpowiedniej składni.
Dzięki
50
Author: Hamed Moghadasi, 2012-01-17
3 answers
Po utworzeniu mapy (co już zrobiłeś, nie musisz powtarzać dla List), jest to tak proste, jak:
List<PersonView> personViews =
Mapper.Map<List<Person>, List<PersonView>>(people);
Możesz przeczytać więcej w dokumentacji AutoMapper dla list i tablic .
100
Author: Justin Niessner,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-11 01:19:14
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-11 01:19:14
Dla Automapera 6
W Uruchomieniu:
Mapper.Initialize(cfg => {
cfg.CreateMap<Person, PersonView>();
...
});
Następnie użyj go w ten sposób:
List<PersonView> personViews = Mapper.Map<List<PersonView>>(people);
14
Author: Ogglas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-19 12:32:09
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-19 12:32:09
Możesz również spróbować w ten sposób:
var personViews = personsList.Select(x=>x.ToModel<PersonView>());
Gdzie
public static T ToModel<T>(this Person entity)
{
Type typeParameterType = typeof(T);
if(typeParameterType == typeof(PersonView))
{
Mapper.CreateMap<Person, PersonView>();
return Mapper.Map<T>(entity);
}
return default(T);
}
3
Author: Antonio Correia,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-15 14:50:10
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-15 14:50:10