[英]How to map several list<property> to view model using automapper
Im trying to figure out to how to map a list to an object containing an identical list property 我试图弄清楚如何将列表映射到包含相同列表属性的对象
I have a view model that looks like this: 我有一个看起来像这样的视图模型:
public class GenerateCodeVM
{
public List<AnimalVM> Animal { get; set; }
public List<RegionVM> Region { get; set; }
}
public class AnimalVM
{
public int? AnimalID { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public DateTime? LastUpdated { get; set; }
}
public class RegionVM
{
public int? RegionID { get; set; }
public string RegionName { get; set; }
public string Code { get; set; }
}
In the controller I have: 在控制器中,我有:
GenerateCodeVM generateCodeVM = new GenerateCodeVM();
AnimalRepository animalRepository = new AnimalRepository();
List<Animal> animal = animalRepository.GetAll().ToList();
RegionRepository regionRepository = new RegionRepository();
List<Region> region = regionRepository.GetAll().ToList();
generateCodeVM = Mapper.Map<List<Region>, GenerateCodeVM> (region);
generateCodeVM = Mapper.Map<List<Animal>, GenerateCodeVM>(animal);
How do I configure the mappings in automapper? 如何在自动映射器中配置映射?
This first bit is fine 这头好
GenerateCodeVM generateCodeVM = new GenerateCodeVM();
AnimalRepository animalRepository = new AnimalRepository();
List<Animal> animal = animalRepository.GetAll().ToList();
RegionRepository regionRepository = new RegionRepository();
List<Region> region = regionRepository.GetAll().ToList();
These next to lines aren't. 这些不是线。
generateCodeVM = Mapper.Map<List<Region>, GenerateCodeVM> (region);
generateCodeVM = Mapper.Map<List<Animal>, GenerateCodeVM>(animal);
I would merely map into the generateCodeVM
object rather than to the whole thing. 我只会映射到
generateCodeVM
对象,而不是整个对象。
So you need to mappings set up. 因此,您需要进行映射设置。
Mapper.CreateMap<Region, RegionVM>();
Mapper.CreateMap<Animal, AnimalVM>();
Then you can use the project
extension method in AutoMapper
. 然后,您可以在
AutoMapper
使用project
扩展方法。 As long as the GetAll()
is returning an IQueryable<>
. 只要
GetAll()
返回IQueryable<>
。
GenerateCodeVM generateCodeVM = new GenerateCodeVM();
AnimalRepository animalRepository = new AnimalRepository();
RegionRepository regionRepository = new RegionRepository();
List<Animal> animal = animalRepository.GetAll().Project().To<AnimalVM>().ToList();
List<Region> region = regionRepository.GetAll().Project().To<RegionVM>.ToList();
Then just assign these projected lists to the generateCodeVM
然后只需将这些投影列表分配给
generateCodeVM
generateCodeVM.Animal = animal;
generateCodeVM.Region = region;
If you don't want to use Project
you can simple do: 如果您不想使用
Project
,则可以执行以下操作:
List<Region> region = regionRepository.GetAll().ToList();
var mappedRegions = Mapper.Map<List<RegionVM>>(region);
You don't need to change the CreateMap
at all to support this, it is supported out of the box. 您根本不需要更改
CreateMap
即可支持此功能,它是开箱即用的。
I would pluralise your variable names that refer to lists, so that it makes your code clearer to you and anyone else looking at it (eg us/me on SO). 我将引用列表的变量名复数,以使您和其他查看它的人(例如,SO上的us / me)的代码更清晰。
List<Region> region = regionRepository.GetAll().ToList();
Should be 应该
List<Region> regions = regionRepository.GetAll().ToList();
This looks like a really odd solution, the last two lines overwrite the same variable. 这看起来是一个很奇怪的解决方案,最后两行覆盖了相同的变量。 I suggest you'd replace the last two lines in the controller with
我建议您将控制器中的最后两行替换为
GenerateCodeVM generateCodeVM = new GenerateCodeVM();
generateCodeVM.Animal = Mapper.Map<List<AnimalVM>>(animal);
generateCodeVM.Region = Mapper.Map<List<RegionVM>>(region);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.