'Using AutoMapper, how do I set a property based on another table's property?

I am having two tables.

Request table
...
UserId
RequesterId
..

User table
Id
Name
..

I need to map this request model into a ViewModel, adding a "User" and "Requester" string property, filled with the corresponding User's names, searched by their ID. RequesterId is the user's ID who submitted the request, UserId is the user's ID who is the request for.

ListRequestViewModel
..
Username
Requestername
..

So far, this is my mapper class code:

public class MapperProfile : AutoMapper.Profile
    {
        public MapperProfile()
        {
            CreateMap<Request, ListRequestViewModel>().ForMember(x => x.Requester, opts => opts.MapFrom(u => u.Requester.Name))
                .ForMember(x => x.RequestedUser, opts => opts.MapFrom(u => u.User.Name));
        }
    }

Of course, this will not work, because I need to find the User, that has the same Id, like the Request's UserId, or RequesterId.

How do I find the users in CreateMap?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source