将字符串字段映射到基于 .net 核心的自动映射器中的通用列表
map string filed to generic list in automapper based on .net core
我有一个 DomainModel
和一个 DTO
像这样:
public class PostEntity: IEntity
{
[Required]
public string Description { get; set; }
public int Id { get; set; }
[Required]
public string Slug { get; set; }
[Required]
public string Tags { get; set; }
[Required]
public string Title { get; set; }
[Required]
public DateTime CreatedOn { get; set; }
public DateTime? UpdatedOn { get; set; }
public PostStatus Status { get; set; }
public User Writer { get; set; }
public int WriterId { get; set; }
}
public class PostDto
{
public int Id { get; set; }
public string Description { get; set; }
public string Slug { get; set; }
public string Tags { get; set; }
public string Title { get; set; }
public DateTime CreatedOn { get; }
public List<string> TagList { get; set; }
public PostDto()
{
TagList = new List<string>();
}
}
PostEntity'Tags
包含一些由 ","
分隔的标签,现在我想用 ","
拆分标签值并将其转换为列表,为此,我试过了但是我得到以下编译错误
CreateMap<PostEntity, PostDto>().ForMember(dest => dest.TagList, cc => cc.MapFrom(src => src.Tags.Split(",").ToList()));
我收到这个错误:
An expression tree may not contain a call or invocation that uses optional arguments
我无法重现你的错误,它似乎工作正常。
下面是正确映射 TagList 的示例
我使用的代码:
MapperConfiguration MapperConfiguration = new MapperConfiguration(configuration =>
{
configuration
.CreateMap<PostEntity, PostDto>().ForMember(dest => dest.TagList, cc => cc.MapFrom(src => src.Tags.Split(',').ToList()));
});
IMapper mapper = MapperConfiguration.CreateMapper();
PostEntity postEntity = new PostEntity
{
Tags = "Tag1,Tag2,Tag3,Tag4"
};
var mappedObject = mapper.Map<PostEntity, PostDto>(postEntity);
请注意 Expression.Call API 不支持可选参数。因此,您应该将 Split(',')
替换为
Split(',', System.StringSplitOptions.None)
或
Split(',', System.StringSplitOptions.RemoveEmptyEntries)
这样做您将不会再看到该错误。
我有一个 DomainModel
和一个 DTO
像这样:
public class PostEntity: IEntity
{
[Required]
public string Description { get; set; }
public int Id { get; set; }
[Required]
public string Slug { get; set; }
[Required]
public string Tags { get; set; }
[Required]
public string Title { get; set; }
[Required]
public DateTime CreatedOn { get; set; }
public DateTime? UpdatedOn { get; set; }
public PostStatus Status { get; set; }
public User Writer { get; set; }
public int WriterId { get; set; }
}
public class PostDto
{
public int Id { get; set; }
public string Description { get; set; }
public string Slug { get; set; }
public string Tags { get; set; }
public string Title { get; set; }
public DateTime CreatedOn { get; }
public List<string> TagList { get; set; }
public PostDto()
{
TagList = new List<string>();
}
}
PostEntity'Tags
包含一些由 ","
分隔的标签,现在我想用 ","
拆分标签值并将其转换为列表,为此,我试过了但是我得到以下编译错误
CreateMap<PostEntity, PostDto>().ForMember(dest => dest.TagList, cc => cc.MapFrom(src => src.Tags.Split(",").ToList()));
我收到这个错误:
An expression tree may not contain a call or invocation that uses optional arguments
我无法重现你的错误,它似乎工作正常。
下面是正确映射 TagList 的示例
我使用的代码:
MapperConfiguration MapperConfiguration = new MapperConfiguration(configuration =>
{
configuration
.CreateMap<PostEntity, PostDto>().ForMember(dest => dest.TagList, cc => cc.MapFrom(src => src.Tags.Split(',').ToList()));
});
IMapper mapper = MapperConfiguration.CreateMapper();
PostEntity postEntity = new PostEntity
{
Tags = "Tag1,Tag2,Tag3,Tag4"
};
var mappedObject = mapper.Map<PostEntity, PostDto>(postEntity);
请注意 Expression.Call API 不支持可选参数。因此,您应该将 Split(',')
替换为
Split(',', System.StringSplitOptions.None)
或
Split(',', System.StringSplitOptions.RemoveEmptyEntries)
这样做您将不会再看到该错误。