开发者

How to select a rectangle from List<Rectangle[]> with Linq

I have a list of DrawObject[]. Each DrawObject has a Rectangle property. 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.

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. 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.


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.

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.

EDIT: Okay, so the above query finds all the drawObjects where the rectangle contains the given location. You almost certainly want to use something like First, FirstOrDefault, Single or 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>. If I'm not all wrong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜