-2

I was trying to map Customer data from List to Customer class the below way but not getting right result. the program is not giving any error but generating customer data with null values.

    using AutoMapper;

    List<Customer> oCust = new List<Customer>();
    oCust.Add(new Customer { Name = "Rajan", Salary = 200, CountryCode = "GB" });
    oCust.Add(new Customer { Name = "Ari", Salary = 200, CountryCode = "US" });
    oCust.Add(new Customer { Name = "Dib", Salary = 200, CountryCode = "CA" });

    var oCustomer = oCust.Where(x => x.CountryCode == "US").ToList(); ;

    Mapper.CreateMap<List<Customer>, Customer>();
    Customer viewModel = Mapper.Map<List<Customer>, Customer>(oCustomer);

I read some article on Automapper from this url http://www.codearsenal.net/2012/12/csharp-object-to-object-mapping-automapper.html#.VKqYbckpp68

Just guide me how to solve the above issue using Automapper only.

Mou
  • 15,673
  • 43
  • 156
  • 275

1 Answers1

2

Like I said here, you don't need AutoMapper for this.

This:

var oCustomer = oCust.Where(x => x.CountryCode == "US").ToList(); 

Will fill a list of customers. Then you want one customer:

Customer viewModel = Mapper.Map<List<Customer>, Customer>(oCustomer);

AutoMapper can do that, but it makes no sense. Which customer should it pick when there are multiple?

Just select one customer, and return that:

Customer viewModel = oCustomer.FirstOrDefault();

If you really think you must map from a list to a single entity (and again, that makes no sense in this scenario), see Mapping from list down to object with AutoMapper.

If you really, really must use AutoMapper to map a single item from a list to a single item and you don't want to use FirstOrDefault() on the list, you can do something like this (pieced together from Projection - AutoMapper Wiki, How to use AutoMapper .ForMember?, Automapper - Does it map lists of objects?):

Mapper.CreateMap<List<Customer>, Customer>()
      .ForMember(t => t.Name, 
                 opt => opt.MapFrom(s => s.FirstOrDefault().Name))
      .ForMember(t => t.Salary, 
                 opt => opt.MapFrom(s => s.FirstOrDefault().Salary))
      .ForMember(t => t.CountryCode, 
                 opt => opt.MapFrom(s => s.FirstOrDefault().CountryCode));

var oCustomer = oCust.Where(x => x.CountryCode == "US").ToList(); ;
Customer viewModel = Mapper.Map<List<Customer>, Customer>(oCustomer);         

Please note this still uses .FirstOrDefault(). Though I will warn you: this code will throw an exception when the source list is empty, isn't easy to maintain and will cause other problems, some of which are fixable using a custom value resolver.

I won't be responsible for you using it and won't support questions following from you using it.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • i agree with u that it make no sense when it can be done with `FirstOrDefault()` but still very curious to see how to resolve it with Automapper. so please give your valuable time to solve it with automapper. thanks – Mou Jan 05 '15 at 14:24
  • 2
    Use your valuable time to educate yourself. Read the link in my answer. – CodeCaster Jan 05 '15 at 14:24
  • very sorry that i read the codeplex link but do not understand how to solve my scenario with `Custom Value Resolvers`. if possible please come with a small sample code for my case. thanks – Mou Jan 05 '15 at 14:28
  • thanks for the answer but i do not understand how to get customer object after populating it from list with automapper? – Mou Jan 05 '15 at 15:08
  • @Mou you already have that code: `Customer viewModel = Mapper.Map, Customer>(oCustomer);` – CodeCaster Jan 05 '15 at 15:09
  • would you please edit your code just to give me the full code. thanks – Mou Jan 05 '15 at 15:24
  • @Mou see edit. Please try to understand the code people give you here, instead of copy-pasting it. – CodeCaster Jan 05 '15 at 15:26
  • yes it worked. thanks a lot but i faced problem to understand this line `ForMember(t => t.Name, opt => opt.MapFrom(s => s.FirstOrDefault().Name))` can u explain what the above line is doing. thanks – Mou Jan 06 '15 at 10:19