简体   繁体   中英

How to split list into LINQ groups and get biggest value in group

I have a List, and I would like to compute a double[360] containing the maximum range along each bearing (to the nearest degree). One hitch is that the chart plotting software I'm using requires each degree to be populated with no holes. I'm aware of the MoreLinq Batch extension, though not quite sure how to use it...

Heres the code I have so far:-

List<PolarCoords> plots = ...;
double chartVals = new double[360]; // unused degrees will be zero

double MaxRangesByDegree = from polar in plots let angle = RadToDeg(polar.Bearing)
                           group plot by angle into degreeBuckets
                           orderby degreeBuckets
                           select MaxRange = (from polar2 in degreeBuckets select polar2.SlantRange).Max().ToArray()

// somehow merge chartVals and MaxRangesByDegree so no holes

I'm probably trying to bite off too much with a single query, and could certainly do it with a simple for loop, but it's a good exercise for learning LINQ:-) At the moment the code is throwing a SystemException: At least one object must implement IComparable...

您提到的异常是由于以下事实:在SlantRange对象之间没有已知的方法来计算Max ,因为SlantRange没有实现IComparable<T>IComparable

var plotsByDegree = plots.ToLookUp(polar => RadToDeg(polar.Bearing));

return Enumerable.Range(0,360)
                 . Select(degree => plotsByDegree.Contains(degree) ? plotsByDegree[degree].Max (polar => polar.SlantRange) : 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM