我需要找到给定纬度和经度的最高峰。我试图修改这个例子以使用类Peak,但不知道如何在IEnumerable<Peak>
而不是IEnumerable<double>.
上使用.Max编译器当然会抱怨
运算符"=="无法应用于类型为"double"one_answers"Peak"的操作数
这是我第一次使用LINQ。如何在IEnumerable<Peak> range?
中找到最大仰角
public class Peak
{
readonly Double latitude;
public Double Latitude { get { return latitude; } }
readonly Double longitude;
public Double Longitude { get { return longitude; } }
readonly Double elevation;
public Double Elevation { get { return elevation; } }
public Peak(Double latitude, Double longitude, Double elevation)
{
this.latitude = latitude;
this.longitude = longitude;
this.elevation = elevation;
}
}
private IList<Peak> FindPeaks(IList<Peak> values, int rangeOfPeaks)
{
var peaks = new List<Peak>();
var checksOnEachSide = rangeOfPeaks / 2;
for (var i = 0; i < values.Count; i++)
{
var current = values[i];
IEnumerable<Peak> range = values;
if (i > checksOnEachSide)
range = range.Skip(i - checksOnEachSide);
range = range.Take(rangeOfPeaks);
if (current.Elevation == range.Max())
peaks.Add(current);
}
return peaks;
}
指定您想要的最大属性:
if (current.Elevation == range.Max(p => p.Elevation))..
请参阅Max<Peak>
用法以查找最大Elevation
:
if (current.Elevation == range.Max(p => p.Elevation))
peaks.Add(current);
我建议你读一点关于lambda表达式的知识,以更好地理解这个sintax是如何工作的。