question

RolandoIvanMedranoChavez-0391 avatar image
0 Votes"
RolandoIvanMedranoChavez-0391 asked YijingSun-MSFT answered

map data from one list to another

Hello,
I have a problem to assign a list of one type of model to another of another type of model What I need to do is the following,
From SQL I get a list with the following fields

Grupo, IdVariable, Nombre, Operador, Condicional

The fact is that I have a model that is adapted to support that structure
My model is the following:

public class ConsultaVariableListadoModel
{
private short _Grupo;
private int _IdVariable;
private string _Nombre;
private string _Operador;
private string _Condicional;


     public short Grupo
     {
         get { return _Grupo; }
         set { _Grupo = value; }
     }

     public int IdVariable
     {
         get { return _IdVariable; }
         set { _IdVariable = value; }

     }

     public string Nombre
     {
         get { return _Nombre; }
         set { _Nombre = value; }
     }

     public string Operador
     {
         get { return _Operador; }
         set { _Operador = value; }
     }

     public string  Condicional
     {
         get { return _Condicional; }
         set { _Condicional = value; }
     }

 }

But I need to group that record by grupo field so create another model

public class GrupoListadoModel
{
private short _Grupo;
private List<ConsultaVariableListadoModel> _ListaConsultaVariable;

     public short Grupo
     {
         get { return _Grupo; }
         set { _Grupo = value; }
     }

     public List<ConsultaVariableListadoModel> ListaConsultaVariable
     {
         get { return _ListaConsultaVariable; }
         set { _ListaConsultaVariable = value; }
     }
 }

I need to assign the records of the first list to the new model, but I don't know how to do it

List<ConsultaVariableListadoModel> arrList = new List<ConsultaVariableListadoModel>();
arrList = this._VariableRepository.FiltrarConsultaVariable(idConsulta);



             List<GrupoListadoModel> arrList2 = new List<GrupoListadoModel>();


             arrList2 = arrList.Select(u => u.Grupo).Distinct();

from the arrayList I want to add the grupo field and make the other fields child,
in advance I appreciate your help

dotnet-csharpdotnet-aspnet-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered

Hi @RolandoIvanMedranoChavez-0391,
As far as I think,you could create a collection and then map to your new model.Just like this:

  var arrList = new List<MapModel>()
                 {
                     new MapModel() { Grupo=1, IdVariable = 1 ,Nombre="AA"},
                     new MapModel() {Grupo=1, IdVariable = 2,Nombre="BB" },
                     new MapModel() {Grupo=2, IdVariable = 13 ,Nombre="CC"},
                 };
                 var result = arrList.Select(
         x => new
         {
             x.Grupo,
             x.IdVariable,
             x.Nombre
         }).ToList();
        
                 var listResults = new List<IndexViewModel>();
                 if (result != null)
                 {
                     listResults.AddRange(result.GroupBy().Select(x => new IndexViewModel
                     {
                         // add to new model
                     }));
                 }
        
             }
             class IndexViewModel
             {
                 public int Grupo { get; set; }
                 public List<MapModel> ListaConsultaVariable { get; set; }
             }

Best regards,
Yijing Sun


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.