4
private Ent FindNearestEntity()
{
    Ent result = null;
    double closestEntDistance = double.MaxValue;

    foreach (var ent in ents)
    {
        if (ent.isEnabled)
        {
            var currentLocation = ent.x;

            var currentDistance = VectorLength(currentLocation);

            if (result == null || currentDistance < closestEntDistance)
            {
                result = ent;
                closestEntDistance = currentDistance;
            }
        }
    }

    return result;
}

How do I achieve the above functionality in a single statement? Here is one of my not so successful attempt.

return ents.Where(e => e.isEnabled).Min<Ent>(e => VectorLength(e.x));
Mc Kevin
  • 962
  • 10
  • 31

0 Answers0