C# LINQ: How to find IList<Peak>.Max?
I need to find the tallest peaks in a given latitude and longiture.
I'm trying to modify this example to work with class Peak but don't understand how to use .Max on IEnumerable<Peak>
rather than IEnumerable<double>.
The compiler, of course, complains
Operator '==' cannot be applied to operands of type 'double' and 'Peak'
This is my first time using LINQ.
How do I find the Max Elevation in 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 = va开发者_如何学Golues[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;
}
Specify the property you want the maximum of:
if (current.Elevation == range.Max(p => p.Elevation))..
See the Max<Peak>
usage to find the max Elevation
:
if (current.Elevation == range.Max(p => p.Elevation))
peaks.Add(current);
I suggest you to read a little about lambda expressions to better understand how this sintax works.
精彩评论