asp.net-mvc 使用 Automapper 映射列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5589471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:05:57  来源:igfitidea点击:

Mapping Lists using Automapper

asp.net-mvcautomapper

提问by Shawn Mclean

I have the classes:

我有课程:

public class Person{ /* Props here */ }

public class PersonViewModel { /* Props here */ }

Then the list:

然后是名单:

List<Person> people = new List<Person>();
List<PersonViewModel> peopleVM = Mapper
                                .MapList<Person, PersonViewModel>(people); //Problem here.

What is the correct way to do this?

这样做的正确方法是什么?

回答by Derek Beattie

Mapper.CreateMap<Person, PersonViewModel>();
peopleVM = Mapper.Map<List<Person>, List<PersonViewModel>>(people);
Mapper.AssertConfigurationIsValid();

From Getting Started:

入门开始

How do I use AutoMapper?

First, you need both a source and destination type to work with. The destination type's design can be influenced by the layer in which it lives, but AutoMapper works best as long as the names of the members match up to the source type's members. If you have a source member called "FirstName", this will automatically be mapped to a destination member with the name "FirstName". AutoMapper also supports Flattening, which can get rid of all those pesky null reference exceptions you might encounter along the way.

Once you have your types, and a reference to AutoMapper, you can create a map for the two types.

Mapper.CreateMap<Order, OrderDto>();

The type on the left is the source type, and the type on the right is the destination type. To perform a mapping, use the Map method.

OrderDto dto = Mapper.Map<Order, OrderDto>(order);

如何使用 AutoMapper?

首先,您需要同时使用源类型和目标类型。目标类型的设计可能受其所在层的影响,但只要成员的名称与源类型的成员匹配,AutoMapper 的效果最佳。如果您有一个名为“FirstName”的源成员,它将自动映射到名为“FirstName”的目标成员。AutoMapper 还支持扁平化,它可以消除您在此过程中可能遇到的所有那些讨厌的空引用异常。

一旦你有了你的类型和对 AutoMapper 的引用,你就可以为这两种类型创建一个映射。

Mapper.CreateMap<Order, OrderDto>();

左边的类型是源类型,右边的类型是目标类型。要执行映射,请使用 Map 方法。

OrderDto dto = Mapper.Map<Order, OrderDto>(order);

回答by Ram

Another Solution

另一种解决方案

List<Person> people = new List<Person>();
List<PersonViewModel> peopelVM;
peopelVM = people.Select(Mapper.Map<Person, PersonViewModel>);

And in the Automapper config

在 Automapper 配置中

Mapper.CreateMap<Person, PersonViewModel>();

回答by Jerph

If you're using IQueryablelists here (from EF or NH, for example) you can use the AutoMapper.IQueryableExtensionsmethods, Project()and To().

如果您在IQueryable此处使用列表(例如来自 EF 或 NH),您可以使用AutoMapper.IQueryableExtensions方法Project()To().

This is my first time with AutoMapper, but I'm succeeding by creating a map for just the model:

这是我第一次使用 AutoMapper,但我成功地为模型创建了一个地图:

Mapper.CreateMap<Person, PersonViewModel>();
Mapper.AssertConfigurationIsValid();

And then using the IQueryableExtensionmethods Project()and To():

然后使用IQueryableExtension方法Project()To()

using AutoMapper.QueryableExtensions;
...

IQueryable<Person> people = new List<Person>().AsQueryable(); //actually from ORM
IQueryable<PersonViewModel> peopleVM = people.Project().To<PersonViewModel>();

回答by ruudj

In core 1.1 this extension might work:

在核心 1.1 中,此扩展可能有效:

public static List<TDestination> MapList<TSource, TDestination>(this IMapper mapper, List<TSource> source)
        {
            return source.Select(x => mapper.Map<TDestination>(x)).ToList();
        }

回答by garryp

You could create an extension method to do something like this using existing mappings for individual items:

您可以创建一个扩展方法来使用单个项目的现有映射来执行这样的操作:

public static class AutoMapperExtensions
{
    public static List<TDestination> MapList<TSource, TDestination>(this IMapper mapper, List<TSource> source)
    {
        return mapper.Map<List<TDestination>>(source);
    }
}

Usage:

用法:

List<PersonViewModel> peopleVM = _mapper.MapList<PersonViewModel>(people);