Update list from another list

0

Working with Visual Studio 2015

I have a list to which I want to update from a list B

foreach (var item in entity.ProductoCatalogos)
            {
                foreach (var asignado in _listProductoCatalogos)
                {
                    asignado.ProductoCatalogoId = item.ProductoCatalogoId;
                    asignado.Id = item.CatalogoId;
                }
            }

I have two records in each list I have noticed that you assign me the data four times that it enters the second foreach what I expected is that as I have 2 records between only two times.

What am I doing wrong?

Greetings!

    
asked by Pedro Ávila 04.08.2018 в 11:13
source

2 answers

0

You can try using LINQ.

_listProductoCatalogos = entity.ProductoCatalogos.select(
       i => new <ObjetoDeLaLista>{ Id  = i.CatalogoId, ProductoCatalogoId = i.ProductoCatalogoId} ).ToList();

link

    
answered by 04.08.2018 в 15:30
0

I could solve it in the following way, you need to first validate for some condition so that you assign the data from one list to another.

foreach (var item in entity.ProductoCatalogos)
            {
                foreach (var asignado in _listProductoCatalogos)
                {
                    if (item.ProductoId == asignado.ProductoId)
                    {
                        asignado.ProductoCatalogoId = item.ProductoCatalogoId;
                        asignado.Id = item.CatalogoId;
                    }
                }
            }
    
answered by 04.08.2018 в 15:35