map data from one list to another

Rolando Ivan Medrano Chavez 1 Reputation point
2021-07-30T18:21:22.297+00:00

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

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,303 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,363 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,071 Reputation points
    2021-08-02T07:41:32.153+00:00

    Hi @Rolando Ivan Medrano Chavez ,
    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.

    0 comments No comments