[英]How to select a rectangle from List<Rectangle[]> with Linq
I have a list of DrawObject[]
. 我有一个DrawObject[]
的列表。 Each DrawObject has a Rectangle
property. 每个DrawObject都有一个Rectangle
属性。 Here is my event: 这是我的活动:
List<Canvas.DrawObject[]> matrix;
void Control_MouseMove ( object sender, MouseEventArgs e )
{
IEnumerable<Canvas.DrawObject> tile = Enumerable.Range( 0, matrix.Capacity - 1)
.Where(row => Enumerable.Range(0, matrix[row].Length -1)
.Where(column => this[column, row].Rectangle.Contains(e.Location)))
.????;
}
I am not sure exactly what my final select command should be in place of the "????". 我不确定我的最终选择命令应该确切地代替“ ????”。 Also, I was getting an error: cannot convert IEnumerable<int> to bool
. 另外,我得到一个错误: cannot convert IEnumerable<int> to bool
。
I've read several questions about performing a linq query on a list of arrays, but I can't quite get what is going wrong with this. 我已经阅读了一些有关在数组列表上执行linq查询的问题,但是我不太明白这是怎么回事。 Any help? 有什么帮助吗?
Edit: 编辑:
Apologies for not being clear in my intentions with the implementation. 抱歉,我对实施的意图不明确。
I intend to select the DrawObject
that currently contains the mouse location. 我打算选择当前包含鼠标位置的DrawObject
。
It's not at all clear what you're trying to do. 尚不清楚您要做什么。 I suspect you want something like: 我怀疑您想要类似的东西:
var drawObjects = from array in matrix
from item in array
where item.Rectangle.Contains(e.Location)
select item;
... but maybe not. ...但也许不是。 You haven't shown what you're trying to do with the result of the query, or what this[column, row]
is there for. 您尚未显示要对查询结果执行的操作或this[column, row]
的用途。
You almost certainly don't want to be using the capacity of the list in the first place - it's more likely that you're interested in the Count
, but using the list as an IEnumerable<T>
is probably better anyway. 几乎可以肯定,您根本不想使用列表的容量 -您更可能对Count
感兴趣,但是无论如何,将列表用作IEnumerable<T>
可能更好。
EDIT: Okay, so the above query finds all the drawObjects
where the rectangle contains the given location. 编辑:好的,所以上面的查询找到了矩形包含给定位置的所有 drawObjects
。 You almost certainly want to use something like First
, FirstOrDefault
, Single
or SingleOrDefault
. 您几乎肯定要使用First
, FirstOrDefault
, Single
或SingleOrDefault
。 For example: 例如:
var drawObject = (from array in matrix
from item in array
where item.Rectangle.Contains(e.Location)
select item)
.SingleOrDefault();
if (drawObject != null) // We found one
{
...
}
var tile = matrix.SelectMany(x => x)
.Where(x => x.Rectangle.Contains(e.Location));
Maybe: 也许:
....Select(y => y);
But it is hard to really tell what you are doing. 但是很难真正分辨出自己在做什么。 And your first Where
clause will not work since the lambda expression in the clause must result in a bool, but your lambda expression is resulting in a IEnumerable<T>
. 而且您的第一个Where
子句将不起作用,因为该子句中的lambda表达式必须导致bool,但是您的lambda表达式却导致IEnumerable<T>
。 If I'm not all wrong. 如果我不是全部都错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.