Function to include and exclude items according to table

Asked

Viewed 55 times

0

I have this function:

 foreach (var item in obj.valores)
        {
            var ver = db.ProdutosFornecedores.Where(r => r.ProdutoID == item.ProdutoID && r.FornecedorID == item.FornecedorID).Select(r => r.Id).Single();
            if (ver == 0)
            {
                ProdutosFornecedores a = new ProdutosFornecedores();
                a.FornecedorID = item.FornecedorID;
                a.ProdutoID = item.ProdutoID;
                db.Add(a);
                db.SaveChanges();
            }
        }
        return Json(new { resultado = 1 });

Which receives the table data, and includes in the database, correctly, but I want it to check with the database data. Example, in the bank it has id 1 and 2, and I’m including 2 and 4, so I need it to delete 1, continue 2, and include 4. I thought to delete all and add again, but I believe it is something unnecessary, it is just a matter of treating, I’m just not able to think the best way to do it, because after the result is realized the submit.

I arrived at this data :

foreach (var item in obj.valores)
            {
                var listaAtual = db.ProdutosFornecedores.Where(r => r.ProdutoID == item.ProdutoID).Select(r => r.FornecedorID).ToList();
                var novaLista = obj.valores.Select(a => a.FornecedorID).ToList();
                var remover = listaAtual.Where(a => !novaLista.Contains(a));
                var adicionar = novaLista.Where(a => !listaAtual.Contains(a));
                foreach (var c in remover)
                {
                    var remove = db.ProdutosFornecedores.Single(d => d.ProdutoID == item.ProdutoID && d.FornecedorID == c);
                    if (remove != null)
                    {
                        db.ProdutosFornecedores.Remove(remove);
                        db.SaveChanges();
                    }
                }
                foreach (var c in adicionar)
                {
                    ProdutosFornecedores a = new ProdutosFornecedores();
                    a.FornecedorID = c;
                    a.ProdutoID = item.ProdutoID;
                    db.Add(a);
                    db.SaveChanges();
                }
            }

But it is not working properly, what is being done wrong? If there is already id = 1, and in the table I include id = 2, it deletes 1 and includes only 2, I need it to remain 1, and includes 2.

1 answer

1


Well, here’s what I’d do. Compare the two lists to know what to remove and what to add. see example:

void Main()
{
    var listaAtual = new List<int>{ 1, 2, 3, 4, 5, 6};
    var novaLista = new List<int>{ 1, 2, 4,6,7};
    var remover = listaAtual.Where(a => !novaLista.Contains(a)).Select(a => a);
    var adicionar = novaLista.Where(a => !listaAtual.Contains(a)).Select(a => a);
    foreach(var c in remover){
        Console.WriteLine(c);
    }
    Console.WriteLine("===");
    foreach (var c in adicionar)
    {
        Console.WriteLine(c);
    }
}
Resultado:
3
5
===
7
  • I edited the question in the form I arrived at, how can I make the comparison, created the two lists, the current list, and the new list.

  • Your logic does not make sense, if in the table there is 1 and you add 2. In the addition list always have to see the ones that will remain and the new ones, so you avoid excluding that already are. and exclude only those who left.

  • It worked, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.