C# - Linq - 获取索引 - 帮助

发布于 2024-11-10 07:37:48 字数 551 浏览 3 评论 0原文

有人可以帮助我处理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紫﹏色ふ单纯 2024-11-17 07:37:48

编辑

这是同一件事的一个更简单的版本:

public int getBoardIndex(int BoardId)
{
    return (from category in this
            from board in category.CategoryBoards
            where board.BoardId == BoardId
            select category.CategoryBoards.ToList().IndexOf(board)).FirstOrDefault();
}

原始版本

要获取其自己类别中第一个匹配板的索引,首先找到类别,然后获取板的索引:

public int getBoardIndex(int BoardId)
{

    var categoryBoard = (from category in this
                         from board in category.CategoryBoards
                         where board.BoardId == BoardId
                         select new {category, board}).FirstOrDefault();
    return categoryBoard.category.CategoryBoards.IndexOf(categoryBoard.board);
}

要获取所有类别中扁平化集合中第一个匹配板的索引,那么@Dan Tao 就有最好的答案。

EDIT

This is a much simpler version of the same thing:

public int getBoardIndex(int BoardId)
{
    return (from category in this
            from board in category.CategoryBoards
            where board.BoardId == BoardId
            select category.CategoryBoards.ToList().IndexOf(board)).FirstOrDefault();
}

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:

public int getBoardIndex(int BoardId)
{

    var categoryBoard = (from category in this
                         from board in category.CategoryBoards
                         where board.BoardId == BoardId
                         select new {category, board}).FirstOrDefault();
    return categoryBoard.category.CategoryBoards.IndexOf(categoryBoard.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.

鹤仙姿 2024-11-17 07:37:48

在我看来你想要这样的东西:

public int getBoardIndex(int BoardId)
{
    var potentialBoards = from category in this
                          from board in category.CategoryBoards
                          select board;

    return potentialBoards.ToList().FindIndex(b => b.BoardId == BoardId);
}

It looks to me like you want something like this:

public int getBoardIndex(int BoardId)
{
    var potentialBoards = from category in this
                          from board in category.CategoryBoards
                          select board;

    return potentialBoards.ToList().FindIndex(b => b.BoardId == BoardId);
}
柳絮泡泡 2024-11-17 07:37:48

到目前为止,您已经有了一个 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).

苏璃陌 2024-11-17 07:37:48

好吧,没有看到对象类,我不能确定,但​​它应该接近于这个:

public static int getBoardIndex(this ObservableCollection<Category> coll, int BoardId)
{
    return coll.IndexOf((
                from category in coll
                from board in category.CategoryBoards
                where board.BoardId == BoardId
                select category).FirstOrDefault());
}

Ok, without seeing the object class, I can't be sure, but it should be close to this:

public static int getBoardIndex(this ObservableCollection<Category> coll, int BoardId)
{
    return coll.IndexOf((
                from category in coll
                from board in category.CategoryBoards
                where board.BoardId == BoardId
                select category).FirstOrDefault());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文