Check items from a list in another list

Asked

Viewed 7,307 times

1

I’m having trouble making a method to check two lists. This method will receive a list which is the list that has been added to the order and the Purchase Order Id to which that order has been associated. The method calls another method that returns from the database the list of items in the purchase request. So, at this point I have 2 lists: one received as a parameter with the items being added to the request, and the other I searched in the database with the items of the request.

I need to check if the items that are in the request are in the request, IE, check that the items in the list I received as 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 has not been purchased I need to mark this property as true and let this item pass to be registered.

I also need to check that these lists have no item that was not requested in the purchase request, 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 requested, must 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’s gotten really big because it’s a complex process but if anyone can help me with it, I’ve tried it all day today, but it’s always a loophole.

Code of the Method

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;
        }
  • 2

    A hint: your question is very difficult to understand, so I imagine you are having problems because you cannot divide the task into parts. Let go of the IDE for a few minutes and try writing - and in English, not in code - what the program should do. Write a sequence of steps. Then highlight the steps you can take and the ones you can’t. This makes you able to isolate the error easier and helps us to help you.

  • 2

    Another thing that might help: have you noticed that several programmers, even older ones, usually have dolls on their desks? Get a doll and try to explain your problem to him. I am serious. It may even be that with this you do not solve your problem, but at least you will have a clearer idea of the causes of your problem.

  • 1

    @Renan I use a rubber duck.

  • @Alanalmeida You can put examples of your code in the question?

  • Thanks for the tips, I’ll see if I can get some dolls put on my table rsrs.. as for the code I’ve touched him so much that now I think he’s already far from reaching the goal, I’m going to tidy him up to leave him the way he was when I got closer to doing what I need, then I update the question.

  • 1

    Renan and Gypsy, I put the code.

  • It’s gotten a lot better and it’s understandable now. Just two more things: First, we don’t know your client’s entire business rule, so we can only analyze the logical part of how the code works. Second, you were able to put together two lists - one for "in" products and one for "out" products. What, in no more than five words, is missing?

  • Look, difficult in less than five words, this is the first version of this method, the problem is, suppose I receive as parameter a list with 3 materials, Ids 1, 2, 3 and in the list that came from the bank the same thing, what happens that in Id one passes normal, but the other two he puts in the list of items that are out of the requisition, ie the other two fall into the LSE and are placed on the list as if they were out of the purchase request.

Show 3 more comments

1 answer

0


Alan,

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

Before I go to the answer itself, another detail, the names make the meaning of your code difficult, so I may suggest names that don’t match the reality or I’ll just reverse the meaning, because I don’t know for sure what your goal is. But I’m going to take that freedom anyway, because I think you could clean up the code a lot, making it a lot less "complex," what I mean is that the complexity of the code doesn’t come from the requirement, but from yourself.

Is there any restriction on the use of Linq in your environment? one of the ways to check whether 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 traded Idmaterial for materials.

Not wanting to "advertise" but I believe to be pertinent, if you want some tips on how to improve the design of your code, take a look at www.chegadegambiarra.com

  • Thanks for the help Breno, sometimes it ends up lacking a little quality in the code because here in the company unfortunately we have a very small team, only 2 and the managers and owners of the company want everything for yesterday.

  • @Alanalmeida You are welcome! reading again my answer, maybe I have caught a little heavy with you. I apologize for that. I hope that you have understood the answer and that you can make use of it to improve part of this algorithm.

  • Rest assured, I meant you were just trying to help and I appreciate the tips.

Browser other questions tagged

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