C# - Linq - 获取索引 - 帮助
有人可以帮助我处理 linq 语句吗,
这就是我到目前为止所拥有的
public class Categories : ObservableCollection<Category>
{
public Categories() { }
public int getBoardIndex(int BoardId)
{
return (from category in this
from board in category.CategoryBoards
where board.BoardId == BoardId
select IndexOf(board)
);
}
}
board 是类别中的一个项目,当我传递 bordId (不是索引)时,它必须在每个类别的所有面板中搜索该 BoardId,然后返回该板的索引
我如何使用 Linq 来做到这一点?
非常感谢您的帮助!
could someone help me with the linq statement,
this is what i have so far
public class Categories : ObservableCollection<Category>
{
public Categories() { }
public int getBoardIndex(int BoardId)
{
return (from category in this
from board in category.CategoryBoards
where board.BoardId == BoardId
select IndexOf(board)
);
}
}
board is an item in category and when i pass a bordId ( which is not an index ) it must search for that BoardId in all boards in each category and then return the Index Of that board
how do i do it using Linq?
Thank you so much for all your help!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑
这是同一件事的一个更简单的版本:
原始版本
要获取其自己类别中第一个匹配板的索引,首先找到类别,然后获取板的索引:
要获取所有类别中扁平化集合中第一个匹配板的索引,那么@Dan Tao 就有最好的答案。
EDIT
This is a much simpler version of the same thing:
Original Version
To get the index of the first matching Board in its own Category, first find the Category, then get the index of the board:
To get the index of the first matching Board in a flattened collection amongst all of the Categories, then @Dan Tao has the best answer.
在我看来你想要这样的东西:
It looks to me like you want something like this:
到目前为止,您已经有了一个 IEnumerable,其中包含所有匹配的板 ID 的索引。
如果您知道只有一个板匹配,则可以调用 .Single() 并返回该单个索引。如果可能有也可能没有匹配的板,那么您可以调用 .ToList() 并将结果分配给变量。然后检查列表中是否有任何项目,然后返回第一个项目或 -1 (或抛出异常,或其他)。
So far, you have an IEnumerable, containing the indices of any board IDs that matched.
If you know that there is exactly one board that matches, you can call .Single() and return that single index. If there might or might not be one board that matches, then you can call .ToList() and assign the result to a variable. Then check whether the list has any items and either return the first item or -1 (or throw an exception, or whatever).
好吧,没有看到对象类,我不能确定,但它应该接近于这个:
Ok, without seeing the object class, I can't be sure, but it should be close to this: