简体   繁体   English

如何映射几个列表 <property> 使用自动映射器查看模型

[英]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;

Hints and Tips 提示和技巧

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.

相关问题 如何使用AutoMapper映射模型属性以查看模型 - How to map model property to view model using AutoMapper 如何 map IEnumerable<model> 到带有 IEnumerable 的 DTO<anotherdto> 使用自动映射器的属性?</anotherdto></model> - How to map IEnumerable<Model> to a DTO with IEnumerable<AnotherDto> property using automapper? 如何映射列表<int>使用 Automapper 转换为复杂类型的属性? - How to map List<int> into property of complex type using Automapper? 使用 AutoMapper CreateMap 将属性映射到对象列表 - Map a property to list of object using AutoMapper CreateMap 使用AutoMapper将XML映射到具有List属性的对象 - using AutoMapper for map XML to Object with List Property 地图ICollection <Model> 列出 <ViewModel> 使用AutoMapper - Map ICollection<Model> to List<ViewModel> using AutoMapper 如何使用 AutoMapper 将可为空的属性映射到 DTO? - How to map a nullable property to a DTO using AutoMapper? 如何使用 Automapper 将原型文件重复类型的 map 属性列出 C# class 的属性? - How to map property of type repeated of proto file to list property of C# class using Automapper? 使用AutoMapper将复杂的视图模型映射回具有集合属性的模型 - Using AutoMapper to map complex viewmodel back to model with collection property 自动映射器:对象列表中的Map属性 - Automapper: Map property in list of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM