Lambda返回包含从函数输出的字段的最小值的对象

时间:2015-12-17 09:33:24

标签: c# lambda

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

如何在单个语句中实现上述功能?这是我不太成功的尝试之一。

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

0 个答案:

没有答案