last day (15 days later) » 

7:11 PM
0
A: Using linq to find the leftmost and rightmost point in a jagged array?

SangramHere is what needs to be done var leftMostVal = strokeCollection.Min(p => p.Point.Min(q => q.Min(r => r.X))); var rightMostVal = strokeCollection.Max(p => p.Point.Max(q => q.Max(r => r.X))); Here is fiddle https://dotnetfiddle.net/y5hZ7w Or public class StrokeCollection : List<Stroke> ...

 
@OndrejJanacek done
 
@Sangram in trying to apply code from dotNetFiddle, how can this be changed IEnumerable<Point> fullList = lst.SelectMany(p => p.Points.SelectMany(q => q.ToList())).ToList(); when lst is of type Point[][] ?? Thanks.
 
@AlanWayne lst is type of StrokeCollection, while fullList is collection of all Points from Stroke(which indeed is jagged array of points) inside StrokeCollection
 
@Sangram Thanks for helping. Please see my above edit. When directly applied as IEnumerable<Point> fullList = myStrokeCollection.SelectMany(p => p.Points.SelectMany(q => q.ToList())).ToList(); p..Points clearly does not exist, so how to change this without introducing any new types?
 
@AlanWayne can you share dotnetfiddle, with whatever you have, I will fix it
 
7:11 PM
@Sangram I've never used dotnetfiddle, so here is a first shot, dotnetfiddle.net/MYiE4x Thank you very much for your time.
 
Hi
here what you need to add
var lstPoints = myStrokeCollection.SelectMany(p => p.ToList()).ToList();
var leftMostPointValue = lstPoints.Min(x => x.X);
var rightMostPointValue = lstPoints.Max(x => x.X);
do u need entire point or just min x (left most point) value & max x (rightmost point)value
/\?
 
Hi
The entire point.
 
here you go
var lstPoints = myStrokeCollection.SelectMany(p => p.ToList()).ToList();

List<IGrouping<double, Point>> l1 = lstPoints.GroupBy(p => p.X).OrderBy(x => x.Key).ToList();
Point leftPoint = l1.First().FirstOrDefault();
Point rightPoint = l1.Last().FirstOrDefault();
 
Hi
Could this be done as one query or non-query linq expression?
(I'm still trying to get my head around SelectMany( ...SelectMany)) ).
 
SelectMany means choose/return multiple items of given type
while Select returns single item
 
7:25 PM
Please correct me.
I'm thinking that since myStrokeCollection is of type Point[][], then the first SelectMany should reduce this to type Point[]. So a second SelectMany would reduce the Point[] to Point ???
And if that is that case, how would one apply Min and Max to the second SelectMany to return the Points with the min and max X?
 
just give me minute
here is finally you need, along with exact type
System.Windows.Ink.StrokeCollection strokeCollection; //Set it to whatever resultant you want collection to set

var l1 = strokeCollection.SelectMany(p => p.StylusPoints)
.GroupBy(p => p.X)
.OrderBy(p => p.Key)
.ToList();

StylusPoint leftPoint = l1.First().FirstOrDefault();
StylusPoint rightPoint = l1.Last().FirstOrDefault();
here is what happens, first select all StylusPoints from different strokes of stroke collection
group them by X co-ordinate
order them by x-coordinate ascending
pick First as minimum one i.e. LeftMost point
pick Last as maximum one i.e. Rightmost Point
Does this solve your question ?
 
Looks good. Give me a minute to test it. Thanks.
 
7:41 PM
Did it worked ?
 
Yes...Problem solved. Thank you very much. I'm still wondering if chaining SelectMany with another SelectMany could not be used?
 
Yes it will
 
How?
 
chaining SelectMany will work if there are nested arrays like
e.g.
Point[][][] arr = new Point[10][][];
var lst = arr.SelectMany(x => x.SelectMany(y => y.ToList()));
3-dimensional arrays
or array of 2-D arrays
or array of array of arrays
Is you query solved ?
 
But not for array of type Point[][]? (This goes for learning, yes, the query solution does work, thanks).
 
7:54 PM
for Point[][] only one SelectMany is required
I have updated answer, Please mark the question resolved :)
Have a good day,
now I am off to Bed
 
Good job sir. Good night.
 

last day (15 days later) »