List of int in delegate Func<> not working

Asked

Viewed 55 times

1

I am studying something and I have come across the following problem. I create a list of 7 elements and treat it within an Func<> works, but if I increase the list items, then my method does not work as it should. You got it: That’s the code that works:

List<int> lista = new List<int> { 1, 2, 3, 4, 5, 6, 7 };

            Func<List<int>, int, bool> f = (itens, item) =>
            {
                if (itens != null && itens.Count > 6)
                {
                    int soma = 0;
                    itens.ForEach(it => soma += it + item);
                    return soma % 2 == 0;
                }
                return false;
            };

            var i = from item in lista
                    where f(lista, item)
                    select item;

            i.ToList().ForEach(ite => listBox1.Items.Add(ite));

However if I change the list by putting two more items or one and I leave Count > 6 or change it, the code still doesn’t work. The result in the listbox should only be even digits. Below does not work, comes the odd digits and not even digits as I would like. What should I do to make it work? If I put the digit "0", then it does not fill anything in the listbox. I just want to understand.

List<int> lista = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            Func<List<int>, int, bool> f = (itens, item) =>
            {
                if (itens != null && itens.Count > 7)
                {
                    int soma = 0;
                    itens.ForEach(it => soma += it + item);
                    return soma % 2 == 0;
                }
                return false;
            };

            var i = from item in lista
                    where f(lista, item)
                    select item;

            i.ToList().ForEach(ite => listBox1.Items.Add(ite));
  • Your code does not add even numbers. It adds numbers whose combinations of a cumulative sum plus a pair of numbers give an even result. I believe the problem is the mathematical formula used.

  • Thanks @Renan, in reality I did not understand or do not understand this line: itens.ForEach(it => soma += it + item);, well I know it’s a foreach inside a lambda, but because adding the variable lambda to the variable sum and item, that’s what I don’t understand and I think that’s the problem.

  • What is the right and wrong result? Are you sure you need to use LINQ? Even if you do, you need to create this Func<>? If you added 2 because only increased one in condition? I think it would be better to describe what you need, because this code is too confusing. What is this sum? Do you only want the pairs that exist on the list? Or is it something else? So it solves? https://dotnetfiddle.net/hjUqg

  • @bigown, the point is I’m studying about delegates and this is exercises from the classes I take. The right result for me is the understanding of all this. Whether it’s even or odd, it doesn’t matter, what I want is to understand this and when I’m accused, have a correct answer.

  • @pnet you made this code or it was shown to you (even the first)?

  • @bigown and everyone else, good morning. This code was shown to me.

  • I can do this in many ways, but the goal is to understand and why the author put the lambda variable in the sum, because that: it => soma += it + item

  • 1

    @pnet it is the list iterator you placed as the first Func<> input parameter, so the value of it will be 1 to 9 in all f(list, item) calls. The variable item will be the second input parameter of Func<>, its value will be 1 in the first call of f(list, item) and then incremented with each call. What really happens on that line is the sum of each integer in the list with item, that is, the variable soma save the result of the foreach.

Show 3 more comments
No answers

Browser other questions tagged

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