简体   繁体   English

将字符串映射到列表<string>反之亦然使用 Automapper

[英]Mapping string to List<string> and vice versa using Automapper

Basically I have this class which represents 1:1 with my database基本上我有这个类,它代表我的数据库 1:1

public class User
{
    public int UserID { get; set; }
    public string Username { get; set; }
    public string Role { get; set; }
}

and I have this viewmodel我有这个视图模型

public class UserEditViewModel
{
    public UserEditViewModel()
    {
        Roles = new List<string>();
    }

    public int UserID { get; set; }
    [Required]
    public string Username { get; set; }
    [Required]
    public List<string> Roles { get; set; }
}

I have no idea how to map between these 2. My current setup :我不知道如何在这两个之间进行映射。我当前的设置:

Mapper.CreateMap<UserEditViewModel, User>().ReverseMap();

There is something similar to your questiong here, please can you check this out AutoMapper: Collection to Single string Property这里有类似于您的问题,请您查看AutoMapper: Collection to Single string Property

PS: This is an example for mapping collection to single string property probably your example should look like below; PS:这是一个将集合映射到单个字符串属性的示例,您的示例可能如下所示;

Mapper.CreateMap<User, UserEditViewModel>()
    .ForMember(dest => dest.Roles,
    m => m.MapFrom(src => src.Role.Split(',').ToList()));

And mapping the instances like below;并映射如下实例;

User myUser = new User();
myUser.Role = "r1,r2,r3,r4,r5";
myUser.UserID = 1;
myUser.Username = "MyUserName";

UserEditViewModel result = Mapper.Map<UserEditViewModel>(myUser);

在此处输入图片说明

2020 Edit: Since Expression.Call API does not support optional parameter and you should Replace src.Role.Split(',') with src.Role.Split(',', System.StringSplitOptions.None) or src.Role.Split(',', System.StringSplitOptions.RemoveEmptyEntries) 2020 编辑:由于Expression.Call API 不支持可选参数,您应该将src.Role.Split(',')替换为src.Role.Split(',', System.StringSplitOptions.None)src.Role.Split(',', System.StringSplitOptions.RemoveEmptyEntries)

To map a string to a collection Cihan Uygun's answer works, only to fix the error将字符串映射到集合Cihan Uygun 的答案有效,只是为了修复错误

CS0854 An expression tree may not contain a call or invocation that uses optional arguments CS0854 表达式树不能包含使用可选参数的调用或调用

which is caused in later updates you should Replace src.Role.Split(',') with src.Role.Split(',', System.StringSplitOptions.None) or src.Role.Split(',', System.StringSplitOptions.RemoveEmptyEntries)这是在以后的更新中引起的,您应该将src.Role.Split(',')替换为src.Role.Split(',', System.StringSplitOptions.None)src.Role.Split(',', System.StringSplitOptions.RemoveEmptyEntries)

To do the reverse mapping just use src => string.Join(',', src.Roles)要进行反向映射,只需使用src => string.Join(',', src.Roles)

Source for the error fix: https://newbedev.com/mapping-string-to-list-string-and-vice-versa-using-automapper错误修复的来源: https : //newbedev.com/mapping-string-to-list-string-and-vice-versa-using-automapper

If you want to concatenate all values in your list of string you need to use string.Join .如果要连接字符串列表中的所有值,则需要使用string.Join In mapper you need to use ForMember method.在映射器中,您需要使用ForMember方法。

From UserEditViewModel to User :UserEditViewModelUser

Mapper.CreateMap<User, UserEditViewModel>().ForMember(user => user.Role, opt => opt.MapFrom(userEdit => string.Join(", ", userEdit.Roles)));

From User to UserEditViewModel :UserUserEditViewModel

Mapper.CreateMap<UserEditViewModel, User>().ForMember(userEdit => userEdit.Roles, opt => opt.MapFrom(user => user.Role.Split(",").ToList()));

I think you should be able to use the AutoMapper AfterMap, something like this:我认为您应该能够使用 AutoMapper AfterMap,如下所示:

.AfterMap((src,dest) => dest.Roles.Add(src.Role))

Credit to the answer here: Map a property to a collection item归功于此处的答案: 将属性映射到集合项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM