How to map a List to another List?

Asked

Viewed 432 times

0

How can I map a list from one type to another list of another type ?

I’m getting an object list of type

public List<DetalheViagem> DetalheViagems { get; set; }

And I need to move the valuables to another list of the kind.

public List<DetalheViagemDto> Viagens { get; set; }

Is there any way to do it without looping?

passagemAprovadaEdiDto.Viagens = new System.Collections.Generic.List<DetalheViagemDto>()
{
   // Incluir a lista de objeto em vez de fazer um a um ...
   new DetalheViagemDto()
   {

   }
};
  • DetalheViagemDto inherits from DetalheViagem or there are only properties in common?

  • Only properties, but not all are with the same name, for example, I have Idviagem in one and the other only Id ...

1 answer

3


Use the Select in the list, it designs a new type from the current type.

Example:

List<DetalheViagemDto> viagens = detalheViagens.Select(t => 
                                 new DetalheViagemDto
                                 { 
                                   Prop1 = t.Prop1, 
                                   Prop2 = t.Prop2,
                                   /*Etc...*/
                                 }).Tolist();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.