Automapper: 如何将包含对象和枚举的列表元组映射为列表。

huangapple go评论70阅读模式
英文:

Automapper: how to map a tuple of list of objects and enum to a list

问题

Here's the code you provided translated into Chinese:

例如,我有第一个对象,它是源

    class PersonEntity
    {
        public string FirstName {get; set;}
        public string LastName {get; set;}
    }
而目标是

    class PersonDto
    {
        public string Name {get; set;}
    }
还有一个枚举

    enum NameMode
    {
        first,
        full
    }

我的映射配置看起来有点像这样

    CreateMap<(PersonEntity, NameMode), PersonDto>()
          .ForMemeber(dest => desat.Name, opt => opt.MapFrom<NameCustomeResolver>());

根据传递的枚举值,我将从源中要么合并名字,要么仅使用名字。现在的问题是,我想使用这个来将一个 PersonEntity 列表映射到一个 PersonDto 列表。

我尝试了这个

    List<PersonEntity> source = new List<PersonEntity>();
    List<PersonDto> destination = mapper.Map<List<PersonDto>>((source, NameMode.full));

我得到了异常

> 缺少类型映射配置或不支持的映射。

经过一些研究,我尝试了以下内容

    var result = source.AsQueryable().Select(p => (p, NameMode.full)).ProjectTo<PersonDto>(mapper.ConfigurationProvider);

但这个有语法错误

> 表达式树可能不包含元组文字

我只想实现将 PersonEntity 列表映射为 PersonDto 列表。

Please note that I've translated the code part of your text.

英文:

For example i have the first object which is the source

class PersonEntity
{
    public string FirstName {get; set;}
    public string LastName {get; set;}
}

and the destination is

class PersonDto
{
    public string Name {get; set;}
}

and an enum

enum NameMode
{
    first,
    full
}

my mapping profile create map looks something like this

CreateMap&lt;(PersonEntity, NameMode), PersonDto&gt;()
      .ForMemeber(dest =&gt; desat.Name, opt =&gt; opt.MapFrom&lt;NameCustomeResolver&gt;()));

where depending on the passed enum value i will either combine first and last name from the source or just use first name, now the issue is that i want to use this to map a list of PersonEntity to a list of PersonDto

i tried this

List&lt;PersonEntity&gt; source = new List&lt;PersonEntity&gt;();
List&lt;PersonDto&gt; destination = mapper.Map&lt;List&lt;PersonDto&gt;&gt;((source, NameMode.full));

i get the exception

> Missing type map configuration or unsupported mapping.

after some research i tried the following

var result = source.AsQueryable().Select(p =&gt; (p, NameMode.full)).ProjectTo&lt;PersonDto&gt;(mapper.ConfigurationProvider);

but this one has the syntax error

> expression tree may not contain tuple literal

i just want to achieve mapping a list of PersonEntity to a list of PersonDto

答案1

得分: 2

You can try this:

var destination = mapper.Map<List<PersonDto>>(source.Select(x => (x, NameMode.full)));

This works because source.Select(x => (x, NameMode.full)) will create an enumerable of tuple (PersonEntity, NameMode), since you already have a mapping configured between (PersonEntity, NameMode) and PersonDto. Automapper will automatically handle mapping between IEnumerable<(PersonEntity, NameMode)> and IEnumerable<PersonDto>.

英文:

You can try this:

var destination = mapper.Map&lt;List&lt;PersonDto&gt;&gt;(source.Select(x =&gt; (x, NameMode.full)));

This works because source.Select(x =&gt; (x, NameMode.full)) will create an enumerable of tuple (PersonEntity, NameMode), since you already have a mapping configured between (PersonEntity, NameMode) and PersonDto. Automapper will automatically handle mapping between IEnumerable&lt;(PersonEntity, NameMode)&gt; and IEnumerable&lt;PersonDto&gt;.

huangapple
  • 本文由 发表于 2023年4月10日 23:18:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定