How to fill a list that has a nested list c #

3

Good afternoon,

I try to fill a list with the following structure:

MAIN LIST- >

[DataContract]

public class Movimientos

{

public string _id, _IdMovimiento, _Hechopor, _Nombre, _Nodoc;

[DataMember]
public List<MovimientoDet> Items { get; set; }

[DataMember]
public string Id
{
    get { return _id; }
    set { _id = value; }
}

[DataMember]
public string Nodoc
{
    get { return _Nodoc; }
    set { _Nodoc = value; }
}

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

[DataMember]
public string Hechopor
{
    get { return _Hechopor; }
    set { _Hechopor = value; }
}

[DataMember]
public string IdMovimiento
{
    get { return _IdMovimiento; }
    set { _IdMovimiento = value; }
}

}

LIST ANIDADA->

[DataContract]

public class MovimientoDet

{

public string _IdProducto, _Producto, _Cantidad, _Lote;

[DataMember]
public string Lote
{
    get { return _Lote; }
    set { _Lote = value; }
}

[DataMember]
public string Cantidad
{
    get { return _Cantidad; }
    set { _Cantidad = value; }
}

[DataMember]
public string Producto
{
    get { return _Producto; }
    set { _Producto = value; }
}

[DataMember]
public string IdProducto
{
    get { return _IdProducto; }
    set { _IdProducto = value; }
}

}

Code to fill the list (It does not work when I try to fill the nested list):

 List<< Movimientos >> lista = new List<< Movimientos >>();

 lista.Add(new Movimientos() {

 Id = "1",

 IdMovimiento = "1345",

 Hechopor = "dsmdk",

 Nombre = "Prueba",

 Nodoc = "Pdsmskdlv",

 Items = ????????

 });    
    
asked by Miguel Rodriguez 26.07.2017 в 22:01
source

1 answer

3

Instead of List<<Movimiento>> must be List<Movimiento> .

To initialize a list within the class Movimiento would be like this:

lista.Add(new Movimientos() {

  Id = "1",
  IdMovimiento = "1345",
  Hechopor = "dsmdk",
  Nombre = "Prueba",
  Nodoc = "Pdsmskdlv",
  Items = new List<MomovientoDto>()
 });    

Only you can not add anything to the list, so I would recommend:

var movimientosDto = new List<MomovientoDto>()
lista.Add(new Movimientos() {
  Id = "1",
  IdMovimiento = "1345",
  Hechopor = "dsmdk",
  Nombre = "Prueba",
  Nodoc = "Pdsmskdlv",
  Items =  movimientosDto 
}); 

movimientosDto.Add(new MovimientoDto{
   IdMovimiento = 44,
   //...
});

Remember that you can also initialize the list with 1 or more objects directly in your declaration . In this case the list (I only put it as an example):

lista.Add(new Movimientos() {

  Id = "1",
  IdMovimiento = "1345",
  Hechopor = "dsmdk",
  Nombre = "Prueba",
  Nodoc = "Pdsmskdlv",
  Items = new List<MomovientoDto>() 
   { 
     new MovimientoDto{  IdMovimiento = 44, HechoPor = "dsmdk" }
   }
 });    

Try to separate each initialization by parts so that you do not get so confused:

List<Movimientos> movimientos = new List<Movimientos>();

// inicializamos una instancia de movimiento
Movimiento mov1 = new Movimiento();
// inicializamos una lista de items dentro de la clase Movimiento
mov1.Items = new List<MovimientoDto>();

// ahora agregamos los MovimientoDto a la propiedad Items
mov1.Items.Add(new MovimientoDto{
 Id = "1",
 IdMovimiento = "1345",
 Hechopor = "dsmdk",
 Nombre = "Prueba",
 Nodoc = "Pdsmskdlv",
});

// y gregamos a la lista movimientos el movimiento y sus items
movimientos.Add(mov1);
    
answered by 26.07.2017 / 22:17
source