You can and should use
v.Last()
instead ofv.ElementAt(v.Count()-1)
. Usingv.Last()
also only iterates through the collection once, which is sometimes something to be aware of (and sometimes a needless microoptimization) and anyway it's less code.
No, I wasn't aware of that, it's good to know.
Edit: And I think you have it all wrong. Your code groups by the X value and sorts the collection of groups by the groups' first Y values. That's what you want?
Er, actually not in theory. The current query seemed to work but maybe it was coincidence. I wanted to sort the Y values (descending) for each group of X values, then choose the first X,Y value (therefore the furthest down on the screen) to feed to another method. Perhaps I'm still confused about the IEnumerables that emerge from the LINQ query and how to access them properly.