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