Sunday, 18 August 2013

Alternatives to nested Select in Linq

Alternatives to nested Select in Linq

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?

No comments:

Post a Comment