如何使用Linq搜索结构数组? [英] How to use Linq in searching an array of structures?
本文介绍了如何使用Linq搜索结构数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Worksheetlayouts数组。每个WorkSheetlayout都有两个Rect,名为hintlocation和listlocation。我需要获得任何一个Rect包含一个Point的左侧和右侧,p。该点只会在其中一个工作表布局的一个矩形中。
我使用以下代码进行两次测试。它可以组合成一个Linq语句吗?
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?
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.
推荐答案
//with linq
var layout = from w in worksheetlayouts
where w.hintlocation.Contains(p) || w.listlocation.Contains(p)
select w;
//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>
>
>
这篇关于如何使用Linq搜索结构数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文