Click here to Skip to main content
16,012,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an array of Worksheetlayouts. Each WorkSheetlayout has two Rect, named hintlocation and listlocation. I need to obtain the Left and Right side of whichever Rect contains a Point, p. The point will only be in one Rect of one of the worksheetlayouts.

I am using the following code which tests twice. Can it be combined into one Linq statement?

SQL
WorksheetLayout[] layout = worksheetlayouts
       .Where( (value, index) => worksheetlayouts[index].hintlocation.Contains(p) || worksheetlayouts[index].listlocation.Contains(p))
       .ToArray();

  double left;
  double right;
  if (layout[0].hintlocation.Contains(p))
  {
      left = layout[0].hintlocation.Left;
      right = layout[0].hintlocation.Right;
  }
  else
  {
      left = layout[0].listlocation.Left;
      right = layout[0].listlocation.Right;
  }


Thank you for any suggestions.
Posted

1 solution

C#
//with linq
var layout = from w in worksheetlayouts
             where w.hintlocation.Contains(p) || w.listlocation.Contains(p)
             select w;






C#
//withou linq
WorksheetLayout layout = worksheetlayouts
       .Where( (value, index) => worksheetlayouts[index].hintlocation.Contains(p) || worksheetlayouts[index].listlocation.Contains(p))
       .FirstOrDefault();

if(layout != null){
left = layout.hintlocation.Left;
right = layout[0].hintlocation.Right;
}

</pre>

>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900