【问题标题】:C# - Linq - get Index on - HelpC# - Linq - 获取索引 - 帮助
【发布时间】:2011-05-27 20:13:01
【问题描述】:

有人可以帮我处理 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)
                    );
    }
}

板是类别中的一个项目,当我传递一个bordId(不是索引)时,它必须在每个类别的所有板中搜索该板ID,然后返回该板的索引

我如何使用 Linq 做到这一点?

非常感谢您的帮助!!!

【问题讨论】:

  • 现在您正在从“this”中进行选择。您是否尝试向对象添加扩展方法?在这种情况下,“这个”是什么?
  • 这是一个 ObservableCollection 的类别
  • 公共类类别:ObservableCollection { public Categories() { } }
  • 索引应该是Board本身Category中的索引,还是所有Category中的索引?例如,Category0 有 3 个板,Category1 有 2 个板。如果我们匹配 Category1 中的第二个板,索引应该是 1 还是 4?
  • 您在类别集合上调用IndexOf(board)。就算编译也找不到。

标签: c# linq indexing indexof


【解决方案1】:

编辑

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

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();
}

原版

要获取自己Category中第一个匹配的Board的索引,先找到Category,然后获取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);
}

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

【讨论】:

    【解决方案2】:

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

    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);
    }
    

    【讨论】:

    • 我没有看到 ToList() 的 FindIndex ?
    • @Kathy: ToList() 应该给你一个List&lt;Board&gt;。 FindIndex 是 List&lt;T&gt; 类的一个方法。
    【解决方案3】:

    到目前为止,您有一个 IEnumerable,其中包含匹配的任何板 ID 的索引。

    如果您知道只有一个匹配的棋盘,您可以调用 .Single() 并返回该单个索引。如果可能有也可能没有匹配的一块板,那么您可以调用 .ToList() 并将结果分配给一个变量。然后检查列表是否有任何项目并返回第一项或 -1(或抛出异常,或其他)。

    【讨论】:

      【解决方案4】:

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

      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());
      }
      

      【讨论】:

      • this ObservableCollection&lt;Category&gt; ?
      • 您将其称为扩展方法。简化示例: var t = new ObservableCollection(); t.getBoardIndex(5);
      • 这会返回类别的索引,而不是板子。
      • @Dave 我认为关键是它不会编译。扩展方法只能在静态类中声明,并且方法本身必须是静态的。此外,即使它是 this,您仍然需要为参数指定一个名称,而不是 this。
      • 仍然无法工作,因为该方法不是静态的,而且我们不知道它在静态类中(而且您还需要coll.IndexOf)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-15
      • 2010-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多