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

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

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

问题

Here's the code you provided translated into Chinese:

  1. 例如,我有第一个对象,它是源
  2. class PersonEntity
  3. {
  4. public string FirstName {get; set;}
  5. public string LastName {get; set;}
  6. }
  7. 而目标是
  8. class PersonDto
  9. {
  10. public string Name {get; set;}
  11. }
  12. 还有一个枚举
  13. enum NameMode
  14. {
  15. first,
  16. full
  17. }
  18. 我的映射配置看起来有点像这样
  19. CreateMap<(PersonEntity, NameMode), PersonDto>()
  20. .ForMemeber(dest => desat.Name, opt => opt.MapFrom<NameCustomeResolver>());
  21. 根据传递的枚举值,我将从源中要么合并名字,要么仅使用名字。现在的问题是,我想使用这个来将一个 PersonEntity 列表映射到一个 PersonDto 列表。
  22. 我尝试了这个
  23. List<PersonEntity> source = new List<PersonEntity>();
  24. List<PersonDto> destination = mapper.Map<List<PersonDto>>((source, NameMode.full));
  25. 我得到了异常
  26. > 缺少类型映射配置或不支持的映射。
  27. 经过一些研究,我尝试了以下内容
  28. var result = source.AsQueryable().Select(p => (p, NameMode.full)).ProjectTo<PersonDto>(mapper.ConfigurationProvider);
  29. 但这个有语法错误
  30. > 表达式树可能不包含元组文字
  31. 我只想实现将 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

  1. class PersonEntity
  2. {
  3. public string FirstName {get; set;}
  4. public string LastName {get; set;}
  5. }

and the destination is

  1. class PersonDto
  2. {
  3. public string Name {get; set;}
  4. }

and an enum

  1. enum NameMode
  2. {
  3. first,
  4. full
  5. }

my mapping profile create map looks something like this

  1. CreateMap&lt;(PersonEntity, NameMode), PersonDto&gt;()
  2. .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

  1. List&lt;PersonEntity&gt; source = new List&lt;PersonEntity&gt;();
  2. 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

  1. 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:

  1. 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:

  1. 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:

确定