0

I have multiple points, and multiple polygons that contain points. I need to match a point to the smallest-area polygon that contains it.

What I get out of the database is a List<(string poly_name, Point point, double area)>.

So I started by doing list.GroupBy(t=>t.point), which gives me a nice IGrouping<Point, (string, Point, double).

Now I want to get the smallest area polygon from each group. Linq's Min() function doesn't allow me to pull the whole tuple; only the smallest area. How can I get the whole tuple?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Kristen Hammack
  • 389
  • 5
  • 16
  • It feels like this is really a duplicate of https://stackoverflow.com/questions/2736236 - the fact that you have groupings and a tuple is somewhat irrelevant; you're still trying to get "the element with the minimum value" from a sequence. – Jon Skeet Sep 10 '19 at 13:41
  • The grouping part is what meant that I couldn't apply another answer to my problem. – Kristen Hammack Sep 11 '19 at 14:42
  • I don't understand what you mean, I'm afraid. If you could provide a [mcve], and show what you've tried (including research into similar questions) that would help us provide a more useful answer, and it would make the question more useful too. – Jon Skeet Sep 11 '19 at 14:44

1 Answers1

0

I came up with an answer while I was writing this question, but I thought it might be useful to someone else. And I'm interested in finding out if the community has a better solution.

list.GroupBy(t => t.point).Select(g => g.OrderBy(t => t.area).First())

Basically, "group the tuples by point, then for each group, order by area, smallest to largest, and only keep the first tuple of the group".

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Kristen Hammack
  • 389
  • 5
  • 16