Lambda to return an object containing the minimum field value that is derived from fuction

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 to achieve the above functionality in one application? Here is one of my failed attempts.

return ents.Where(e => e.isEnabled).Min<Ent>(e => VectorLength(e.x));
+4
source share

Source: https://habr.com/ru/post/1620515/


All Articles