英文:
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<(PersonEntity, NameMode), PersonDto>()
.ForMemeber(dest => desat.Name, opt => opt.MapFrom<NameCustomeResolver>()));
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<PersonEntity> source = new List<PersonEntity>();
List<PersonDto> destination = mapper.Map<List<PersonDto>>((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 => (p, NameMode.full)).ProjectTo<PersonDto>(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<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>
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论