L
L
LemanRass212018-11-01 01:00:58
C++ / C#
LemanRass21, 2018-11-01 01:00:58

Why doesn't a similar implementation via LINQ query work?

Hello.
I don’t seem to understand something about Predicate, but I really don’t catch up with why this code works:

for (int i = 0; i < Beads.Count; i++)
            if (Vector3.Distance(Beads[i].position, pos) < 0.1f)
                return false;
        return true;

and this one, which seems similar to me, is not:
return Beads.Any(n => Vector3.Distance(n.position, pos) < 0.1f);

I would be very grateful for the explanations.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlov, 2018-11-01
@LemanRass21

// Аналогично циклу с использованием Any
return !Beads.Any(n => Vector3.Distance(n.position, pos) < 0.1f);
// Можно с использованием All
return Beads.All(n => !Vector3.Distance(n.position, pos) < 0.1f);

In the first loop false, returns if the condition is met at least once. Anyin this case will return true. So you need to apply inversion. You can also rewrite the operation using the All.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question