Home > Mobile >  Set property value with automapper to combination with 2 other
Set property value with automapper to combination with 2 other

Time:11-18

I am having employee table:

public class Employee
{
   public int Id {get;set;}
   public string FirstName {get;set;}
   public string LastName {get;set;}
   public int SupervisorId {get;set;}
}

SupervisorId is a foreign key from the same table (Employee) that points to other employee.

Then I have something like "EmployeeSupervisorDto"

public class EmployeeSupervisorDto
{
   public int Id {get;set;}
   public string FirstName {get;set;}
   public string LastName {get;set;}
   public string FullNameSupervisor {get;set;}
}

What I want to achievie is to use automapper to set FullNameSupervisor automaticly to combination of FirstName and LastName of supervisor.

Tried so far to do something like this:

cfg.CreateMap<Employee, EmployeeSupervisorDto>()
    .ForMember(e => e.FullNameSupervisor, m => m.MapFrom(p => $"{p.LastName} {p.FirstName}"));

But I have no idea how to do reference to Id that points out to employee id that is supervisor of given employee.

CodePudding user response:

To use the below solution, you will need to inject your data context to the auto mapper profile class (via constructor parameter), and also, in the ConfigureServices, add the DI of the automapper profile as shown in https://stackoverflow.com/a/49198279/9907597.

  1. Create a method in the AutoMapper profile class:

    public string GetEmployerFullName(int supervisorEmpId)
    {
         var supervisor = db.Employees.Find(supervisorEmpId);
         return supervisor.FirstName   " "   supervisor.LastName;
    }
    
  2. Then create the mapping in the automapper profile class constructor as:

    CreateMap<Employee, EmployeeSupervisorDto>()
        .ForMember(e => e.FullNameSupervisor, m => m.MapFrom(p => GetEmployerFullName(p.SupervisorId)));
    

CodePudding user response:

You can use either ValueResolver or something like this code if you want to use it once and not generally:

Mapper.CreateMap<Employee, EmployeeSupervisorDto>()
.ForMember(e => e.FullNameSupervisor, o => o.ResolveUsing(m => { return  m.LastName   m.FirstName}));

CodePudding user response:

May help you, i try with linq approach :

public class Employee
{
   public int Id {get;set;}
   public string FirstName {get;set;}
   public string LastName {get;set;}
   public int SupervisorId {get;set;}
} 

it give you the list of employe with sup FullNameSupervisor

var Employeelist = new List<Employee>() {...};

var EmployeeWithSup = from ep in Employeelist 
                      join epsup in Employeelist on ep.SupervisorId equals epsup.Id
                      select new { Id = ep.Id,FirstName = ep.FirstName,LastName = ep.LastName,
                      SupervisorId = ep.SupervisorId,FullNameSupervisor = epsup.FirstName   " "   epsup.LastName };

If you want to do joins use automapper ,try the follow link : AutoMapper to join records from 2 tables into single IEnumerable viewmodel

  • Related