How to Convert ICollectiont1 in ICollectiont2

0

I have a LinQ To Entities Query,

And an ActivityModel returns to me, but it really does the query to the Activity entity, and I assign each data as you can see it, but it brings me the Orders as Icollection<Orden> , and in ActivityModel I have it as ICollection<OrdenModel> What can I do to convert it ?, with the best possible practice, and if you can improve the code it would be fine thank you!

var result = (from a in ctx.Actividad
                          join e in ctx.Empleado on a.IdEmpleado equals e.IdEmpleado
                          where a.IdEstimacion == id
                          select new ActivityModel()
                          {
                              Responsable = e.Nombre,
                              IdEmpleado = a.IdEmpleado,
                              IdActividad = a.IdActividad,
                              IdEstimacion = a.IdEstimacion,
                              OrdenModels = a.Ordenes,
                              Nombre = a.Nombre,
                              FechaFinal = a.FechaFinal,
                              FechaInicial = a.FechaInicial,
                              HorasPresupuestadas = a.HorasPresupuestadas
                          }).ToList();
    
asked by Kmiilo Berrio Montoya 25.10.2016 в 23:57
source

2 answers

2

You can use automappers ! link . This to map the entity you want.

What you could do is a function that returns that collection, and within the function use automappers . It's easy to use and saves you a lot of code.

    
answered by 26.10.2016 в 01:26
1

I think if you do:

var collection = Collection<OrdenModel>
select new ActivityModel()
{
    Responsable = e.Nombre,
    IdEmpleado = a.IdEmpleado,
    IdActividad = a.IdActividad,
    IdEstimacion = a.IdEstimacion,
    OrdenModels = collection.add
    (
        new OrdenModel{ prop1 = a.value. prop2 = a.value2}
    ),
    Nombre = a.Nombre,
    FechaFinal = a.FechaFinal,
    FechaInicial = a.FechaInicial,
    HorasPresupuestadas = a.HorasPresupuestadas
}).ToList();

I have not tried the code, but if the properties are similar, you can map them, then it will be simple, because when the interaction finishes you will already have the values you need associated with the OrdenModels collection.

    
answered by 26.10.2016 в 16:52