I have a square shape like this that is devided to 9 rectangles:

So I want to instead of making a long list of like this:
if(rectForward.Contains(touchPoint))
{
return rectForward;
}
else if(rectForwardRight.Contains(touchPoint))
{
return rectForwardRight;
}
//and so on!
Quickly check with a linq query to see which Rectangle contains the Point touchPoint.
Well I could use the time that I spend on this question to write the if/else stuff but I would like to see how it goes with linq
Try storing all your rectangles in a collection, then it becomes trivial to iterate over them:
Depending on what you want to do if the point is not contained in any of them, you may want to use
Firstonly (which would throw an exception if the point isn’t contained by any of them);FirstOrDefaultwill (assuming you’re using theSystem.Windows.Rectclass) return a defaultRect(located at0,0with0size) so that may be difficult to check for.If you don’t want to store the rectangles in a collection, you can write a simple helper method to iterate over them in sequence and leverage that when you wish:
To which your Linq call would use: