需要帮助完成或重写此算法以导航泛型集合

本文关键字:算法 导航 泛型 集合 重写 帮助 | 更新日期: 2023-09-27 18:10:08

我正试图想出一个算法的代码,以根据用户是否单击"前进"按钮或"后退"按钮来设置当前对象。

public Step CurrentStep
{
    get { return _currentStep; }
    set
    {
        if (_currentStep != value)
        {
            _currentStep = value;
            OnPropertyChanged("CurrentStep");
        }
    }
}
private int CurrentStepIndex { get; set; }
private void NextStep()
{
    CurrentStepIndex++;
    GotoStep();
}
private void PreviousStep()
{
    CurrentStepIndex--;
    GotoStep();
}
private void GotoStep()
{
    var query = from step in CurrentPhase.Steps
                where ????
                select step;
    CurrentStep = query.First();
}

CurrentPhase.StepsObservableCollection<Step> Steps {get; set;}。在这个类的构造函数中,我有一种为属性"CurrentStep"设置默认值的方法,所以总是有一个可以启动的。

给定此集合,我希望使用存储在CurrentStepIndex中的CurrentStep对象的索引来查找此项目在集合中的位置,然后通过递减或递增来更改该索引。然后,使用某种linq查询,在新索引处找到"下一步"。

不幸的是,我很难制定我的LINQ查询。更重要的是,我不确定这个算法是否有效。

我需要什么来完成我的LINQ查询,使这个算法工作?

或者,有没有更好的方法来实现我想要的?

需要帮助完成或重写此算法以导航泛型集合

这里没有必要使用LINQ。ObservableCollection从Collection它有Items属性(c#中的索引器)。这意味着您可以使用以下代码来代替LINQ:

private void GotoStep()
{
    CurrentStep = CurrentPhase.Steps[CurrentStepIndex];
}

使用以下内容,但要确保控制溢出

  if(CurrentStepIndex>=0 && CurrentStepIndex<CurrentPhase.Steps.Count)
  {
   CurrentStep= CurrentPhase.Steps[CurrentStepIndex)
  }