1

I have two classes:

public class Customer
{
    public string FirstName { get; set;  }

    public string LastName { get; set; }
};

public class Customer_
{
    public string FirstNam { get; set; }

    public string LastNam { get; set; }
}

And one instance of my class Customer:

Customer[] Customer = new Customer[]
{
    new Customer()
    {
        FirstName = "FirstName1",
        LastName = "LastName1"
    },
    new Customer()
    {
        FirstName = "FirstName2",
        LastName = "LastName2"
    }
};

I want to map my object Customer to an instance of my object Customer_.

I use Automapper:

var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<Customer, Customer_>();
});

IMapper mapper = config.CreateMapper();
var dest = mapper.Map<Customer[], Customer_[]>(Customer);

But the object dest is have two index null

How to create the map and and define mappings ? For example : Customer.FirstName = Customer_.FirstName ...etc

Edit :

I add a new property Quantity (type int) in my class Customer and a value in my instance. Now, I want to map only customer who have a quantity > x

Customer[] Customer = new Customer[]
{
    new Customer()
    {
        FirstName = "FirstName1",
        LastName = "LastName1",
        Quantity = 11
    },
    new Customer()
    {
        FirstName = "FirstName2",
        LastName = "LastName2",
        Quantity = 5
    }
};

How do to ?

2
  • Why are the properties in the second type spelled differently? Commented May 14, 2016 at 15:20
  • For to test Automapper :) Commented May 14, 2016 at 15:27

1 Answer 1

1

The documentation outlines this fairly clearly.

Because the names of the destination properties do not exactly match the source property ..., we need to specify custom member mappings in our type map configuration:

cfg.CreateMap<Customer, Customer_>()
   .ForMember(dest => dest.FirstNam , opt => opt.MapFrom(src => src.FirstName))
   .ForMember(dest => dest.LastNam , opt => opt.MapFrom(src => src.LastName))

Note that you have to explicitly map in the other direction if necessary as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.