tags:

views:

70

answers:

2

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...

A: 

The exception you mention is due to the fact that there is no known way to calculate Max among SlantRange objects, because SlantRange does not implement IComparable<T> or IComparable.

Jay
Ok, so I need to make SlantRange a Double instead of a double, or cast it inside the select statement:-) Do you know if there is any way to use the Batch method in a query expression or do I have to convert it to a method based query?
Sean Donohue
A: 
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);
Ani
That's brilliant:-) I don't have to worry about the MoreLinq extensions now - I wouldn't have thought of using the ToLookup, and though I knew about Range(0,360) I wasn't sure how to combine it with the plot data. Thanks very much:-)
Sean Donohue
No worries. Just took a guess about what your requirement was, didn't even know what to choose as as sensible default since I don't know what 'SlantRange' means!
Ani