0

I have Source Object which will be like below,

Class Employee
{
   int EmpID { get; set; }
   string EmpName { get; set; }
   List<AddressRelation> AddressRelations { get; set; }
}

Class AddressRelation
{
   int AddressRelationId { get; set; }
   int AddressTypeId { get; set; }
   int AddressId { get; set; }
}

Class Address
{
   int AddressId { get; set; }
   string AddressLine1 { get; set; }
   string AddressLine2 { get; set; }
   string City { get; set; }
   string State { get; set; }
   string Zip { get; set; }
}

**Destination Object**

Class EmployeeDTO
{
   int EmpID { get; set; }
   string EmpName { get; set; }
   List<AddressDTO> Addresses { get; set; }
}

Class AddressDTO
{
   int AddressId { get; set; }
   string AddressLine1 { get; set; }
   string AddressLine2 { get; set; }
   string City { get; set; }
   string State { get; set; }
   string Zip { get; set; }
}

Now, the Source Object has AddressRelation collection, inside that list of Address will be there.. means there will be 0-n AddressRelation for an employee and inside each AddressRelation there will be 0-n Address.

Now, I want to map only the Addresses from Source Object and Assign it to Destination Object's Address collection using Automapper. means iterate each AddressRelation, take the Addresses and assign to Destination Object's Address Collection. how to do this using AutoMapper ?

Thanks in advance, Prakash.

PatrickSteele
  • 14,489
  • 2
  • 51
  • 54
ps_prakash02
  • 543
  • 4
  • 18
  • The problem I see is that `AddressRelation` doesn't have a reference to an `Address` object. Will there be a collection of `Address` records somewhere available to Automapper? – PatrickSteele Apr 18 '16 at 14:39
  • AddressId is the reference between Address and AddressRelation. – ps_prakash02 Apr 19 '16 at 06:53
  • Yeah, I kind of guessed that. But `AddressRelation` has no direct reference to an `Address` object (at least not in the class definition). Where would Automapper look to find the `Address` record based on the `AddressRelation.AddressId`? – PatrickSteele Apr 19 '16 at 11:23
  • Yes.. In destination type (Employee) we don't have AddressRelation but we have Address collection.. so from the model, I need to iterate all the AddressRelation and copy the Address to destination type.. Is this can be automatically done by AutoMapper ? – ps_prakash02 Apr 19 '16 at 11:27
  • I don't think you're understanding my question. First off, yes, Automapper can do it. BUT, Automapper will need access to the collection of `Address` records. _Where will those come from_? – PatrickSteele Apr 19 '16 at 11:58
  • DestType->AddressCollection.Add(SourceType -> AddressCollection[0]->Address); DestType->AddressCollection.Add(SourceType -> AddressCollection[1]->Address); – ps_prakash02 Apr 19 '16 at 12:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109565/discussion-between-ps-prakash02-and-patrick-steele). – ps_prakash02 Apr 19 '16 at 14:00
  • If there was an answer discovered by you guys in your chat session, I'd sure love to see it. :) – Scott Fraley Jun 13 '19 at 19:54

0 Answers0