Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6635147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:00:54+00:00 2026-05-25T23:00:54+00:00

I would like to use something like AutoMapper for the following code instead of

  • 0

I would like to use something like AutoMapper for the following code instead of manual mapping.

I have two tables User and Role and both mapped with RoleID. now problem is if i am getting records from Users then it will gives me only RoleID instead of RoleName. so i tried to get result with custom class and mapped each entities with for loop (Manually).

Can anybody help to optimize this manual code to automatic. because i have more then 15 tables with relations.

// M O D E L
public class RoleModel : IRoleModel
{
    public Guid RoleID { get; set; }
    public string RoleName { get; set; }
    public string Description { get; set; }
}
public class UserModel : IUserModel
{
    public Guid UserID { get; set; }
    public Guid RoleID { get; set; }
    public string UserName { get; set; }
}
public class UserRoleModel
{
    public Guid UserID { get; set; }
    public string UserName { get; set; }
    public string Description { get; set; }
    public Guid RoleID { get; set; }
    public string RoleName { get; set; }
}
// C O N T R O L L E R
public class UsersController : Controller
{
    private IUserService _UserService;
    public UsersController()
        : this(new UserService())
    {
    }
    public UsersController(IUserService UserService)
    {
        _UserService = UserService;
    }
    public ActionResult Index()
    {
        IList<UserRoleModel> users = _UserService.GetUsersWithRole();
        return View(users);
    }
    public ActionResult Create()
    {
        return View();
    }
}
// S E R V I C E
public class UserService : ServiceBase<IUserService, User>, IUserService
{
    private IUserRepository _UserRepository;
    public UserService()
        : this(new UserRepository())
    {
    }
    public UserService(IUserRepository UserRepository)
    {
        _UserRepository = UserRepository ?? new UserRepository();
    }
    public IList<UserRoleModel> GetUsersWithRole()
    {
        IList<User> users = _UserRepository.GetAll();
        IList<UserRoleModel> userswithrol = new List<UserRoleModel>();
        /* I would like to use AUTO MAPPER instead of MANUAL MAPPING*/
        foreach (User u in users)
        {
            UserRoleModel ur = new UserRoleModel();
            ur.UserID = u.UserID;
            ur.UserName = u.UserName;
            ur.Description = u.Description;
            ur.RoleID = u.RoleID.Value;
            ur.RoleName = u.Role.RoleName;
            userswithrol.Add(ur);
        }
        /**/
        return userswithrol;
    }
    private IList<UserModel> GetAll()
    {
        IEnumerable<User> alliance;
        if (whereCondition != null)
            alliance = _UserRepository.GetAll(whereCondition);
        else
            alliance = _UserRepository.GetAll();
        UserListModel model = new UserListModel();
        model.UserList = new List<UserModel>();
        AutoMapper.Mapper.CreateMap<User, UserModel>().ForMember(dest => dest.UserID, opt => opt.MapFrom(src => src.UserID));
        model.UserList = AutoMapper.Mapper.Map(alliance, model.UserList);
        return model.UserList;
    }
}

Any answer would be appreciated!
Thanks,
Imdadhusen

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:00:54+00:00Added an answer on May 25, 2026 at 11:00 pm

    I have solved this question using following solution:

    public IList<UserRoleModel> GetUsersWithRole()
            {
                IList<User> users = _UserRepository.GetAll();
                IList<UserRoleModel> userswithrol = new List<UserRoleModel>();
                AutoMapper.Mapper.CreateMap<User, UserRoleModel>()
                    .ForMember(d => d.UserID, o => o.MapFrom(s => s.UserID))
                    .ForMember(d => d.RoleName, o => o.MapFrom(s => s.Role.RoleName));
                userswithrol = AutoMapper.Mapper.Map(users, userswithrol);
                return userswithrol;
            }
    

    Note: i have just added single line of code to achieve desired output

     .ForMember(d => d.RoleName, o => o.MapFrom(s => s.Role.RoleName))
    

    Thanks,
    Imdadhusen

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use something like the following: SELECT city FROM cities WHERE
I would like to use NHibernate Automapper to map the following class: public class
I would like to use something like CLR Profiles on .Net 2.0 to see
I am using xampp on Windows, but I would like to use something closer
I would like to use Dictionary as TKey in another Dictionary . Something similar
Should I use something like maven for this? Or would finding them as they
I would like something that I can use as follows var msg = new
I would like to do something like this i.e., use wild card characters in
I would like to use something like this: Dictionary<int, string>[] matrix = new Dictionary<int,
I would like to use something like float:left css on inside with specified width

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.