Alternatives to nested Select in Linq
c#, linq
Solution
The problem definition, that you want "for each point, the set of other points" makes it impossible to solve without the inner query - you could just disguise it in clever manner. If you could change your data storage policy, and don't stick to LINQ then, in general, there are many approaches to Nearest Neighbour Search problem. You could for example hold the points sorted according to their values on one axis, which can speed-up the queries for neighbours by eliminating early some candidates without full distance calculation. Here is the paper with this approach: Flexible Metric Nearest Neighbor Classification.
Problem
Working on a clustering project, I stumbled upon this, and I'm trying to figure out if there's a better solution than the one I've come up with. PROBLEM : Given a `List<Point> Points` of points in R^n ( you can think at every Point as a double array fo dimension n), a `double minDistance` and a distance `Func<Point,Point,double> dist` , write a LINQ expression that returns, for each point, the set of other points in the list that are closer to him than minDistance according to dist. My solution is the following: ``` var lst = Points.Select( x => Points.Where(z => dist(x, z) < minDistance) .ToList() ) .ToList(); ``` So, after noticing that - Using LINQ is probably not the best idea, because you get to calculate every distance twice - The problem doesn't have much practical use - My code, even if bad looking, works I have the following questions: - Is it possible to translate my code in query expression? and if so, how? - Is there a better way to solve this in dot notation?