Check items from a list in another list

1

I'm having a hard time making a method to check 2 lists. This method will receive a list that is the list that was added to the order and the purchase requisition ID to which that order was associated. The method calls another method that returns the list of items in the purchase requisition from the bank. So, at this point I have 2 lists: one received as a parameter with the items that are being added to the request, and another one that I searched the database for the requisition items.

I need to check if the items that are in the order are in the request, ie check if the items in the list that I received as a parameter are in the items in the list that came from the bank. If so, I need to check if this item has already been purchased (I have a Boolean property for this), and if it was not purchased I need to mark this property as true and let this item pass to be registered.

I also need to check if these items do not have any items that were not requested in the purchase requisition, ie the buyer can not buy without a requisition for this item.

All items that should not be in the order, either because it was already purchased or because it was not ordered, should be removed by the system. After registering the request, I have to display the list of items that were removed by the system.

I know it got really big because it's a complex process but if anyone can help me with it, I tried it all day today, but there's always a loophole for errors.

Method Code

public ControlePedidoRCM ControlePedidoRCM(int IdRCM, List<MaterialPedido> IdMaterial)
        {
            RCMDal rd = new RCMDal();
            ControlePedidoRCM Controle = new ViewModel.ControlePedidoRCM();
            List<int> Comprados = new List<int>();
            List<int> Fora = new List<int>();

            List<MaterialRCM> itens = rd.ListarItens(IdRCM).ToList();

            foreach(var item in itens)
            {
                for(int i = 0; i < IdMaterial.Count; i++)
                {
                    //se o material estiver na lista de RCM
                    if(item.MaterialID == IdMaterial[i].MaterialID)
                    {
                        if(item.Comprado == true)
                        {
                            //ja foi comprado
                            Comprados.Add(item.MaterialID);
                        }
                    }
                    else
                    {
                        //não está na RCM
                        Fora.Add(item.MaterialID);
                    }
                }
            }
            Controle.Comprado = Comprados;
            Controle.Fora = Fora;
            return Controle;
        }
    
asked by anonymous 30.10.2014 / 21:01

1 answer

0

Alan,

It's easier for other developers to understand your code when you follow conventions, parameter in .NET starts with lowercase letters, and despite the strange names, I hope I can answer your question. (ah, and before I forget, there is no need to compare "== true", the compiler is smarter than it sounds)

Before I go to the answer itself, another detail, the names make it difficult to understand the meaning of your code, so it might be that I suggest names that do not fit into reality or just reverse the meaning, sure what your goal is. But I'm going to take that liberty anyway, because I think you could clean up the code a lot, making it far less "complex," which is to say that the complexity of the code comes not from the requirement but from yourself.

Is there any restriction on using Linq in your environment? one of the ways to check if elements are part of a list would be as follows:

var idDosMateraisNoCatalogo = rd.ListarItens(IdRCM).ToList().Select(m => m.MaterialID);

var materiaisNaListaDeRCM = materiaisRequisicao.Where(m => idDosMateraisNoCatalogo.Contains(m.MaterialID));

var materiaisForaDaListaDeRCM = materiaisRequisicao.Where(m => !idDosMateraisNoCatalogo.Contains(m.MaterialID));

In this case, I changed IdMaterial for materialsRequisition.

Not wanting to advertise, but I think it's relevant, if you'd like some tips on how to improve your code design, check out www.chegadegambiarra.com

    
04.11.2014 / 16:18