AutoMapper : As my understanding, it is impossible to map AddToClientCommand
to List<AddToClient>
. because the AotuMapper provide 2 ways for mapping such as following...
For Example: We have 2 classes like Employee and User
public class Employee
{
public int EmployeeId { get; set; }
public string EmployeeFName { get; set; }
public string EmployeeLName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public DateTime? DateOfJoining { get; set; }
}
public class User
{
public int Userid { get; set; }
public string UserFName { get; set; }
public string UserLName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public DateTime? DateOfJoining { get; set; }
}
Employee objEmployee = new Employee
{
EmployeeId = 1001,
EmployeeFName = "Manish",
EmployeeLName = "Kumar",
Address = "JAIPUR",
City = "JAIPUR",
State = "RAJASTHAN",
Zip = "302004",
DateOfJoining = DateTime.Now,
};
//1. Creates the map and all fields are copied if properties are same
Mapper.CreateMap<Employee, User>();
//2. If properties are different we need to map fields of employee to that of user as below.
AutoMapper.Mapper.CreateMap<Employee, User>()
.ForMember(o => o.Userid, b => b.MapFrom(z => z.EmployeeId))
.ForMember(o => o.UserFName, b => b.MapFrom(z => z.EmployeeFName))
.ForMember(o => o.UserLName, b => b.MapFrom(z => z.EmployeeLName));
User objuser = Mapper.Map<Employee, User>(objEmployee);
// But your requirement will be fullfill through Linq Query...
AddToClientCommand addCommand = new AddToClientCommand
{
AccountIds = new List<int> { 1, 2 },
ClientId = 42
};
List<AddToClient> addClientList = addCommand.AccountIds.Select(item => new AddToClient { AccountId = item, ClientId = addCommand.ClientId }).ToList();
Output:

