I don't think LINQ is the best tool for this kind of job, but it can be achieved. The important part is to think of the relation between your Point.X
and the index of the relative Point
in the Point.Y
group. Once you realize you want to group them by Point.X - Index
, you can do:
var Hgroup = pointcloudH.OrderBy(p => p.Y)
.GroupBy(p => p.Y)
.SelectMany(yGrp =>
yGrp.Select((p, i) => new {RelativeIndex = p.X - i, Point = p})
.GroupBy(ip => ip.RelativeIndex, ip => ip.Point)
.Select(ipGrp => ipGrp.ToList()))
.ToList();
Note that this will probably perform worst than a regular iterative algorithm. My pointcloudH is an array, but you can just change the lambda to reflect your own list. Also, remove the ToList()
if you want to defer execution. This was to ease the result inspection in the debugger.
If you want to group all points in a Point.Y
group regardless of their index (ie order by Point.X
as well. Add ThenBy(p => p.X)
after the first OrderBy
clause.