简体   繁体   中英

AutoMapper: how map very complex object

I have a Chat object with a Users field. It's an array ( IQueryable ) of User objects.

Each User object has fields like Name , E-mail , etc.

How can I map this Chat object in a way that ChatDTO object will have the field Users available as a string? I want the string to be only the names of users separated by comma.

That is, how do I transform an array of objects to one string?

Should look something like this:

Mapper.CreateMap<Chat, ChatDTO>()
    .ForMember(
        dest => dest.Users, 
        opt => opt.MapFrom(
            src => string.Join(",", src.Users.Select(u => u.Name))));

Assuming the following classes:

public class Chat
{
    public IQueryable<User> Users { get; set; }
}

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

    public string Email { get; set; }
}

public class ChatDTO
{
    public string Users { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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