获取列表中具有当前 ID 的 object 之后的下一个 object

Get the next object in list after the object with current ID

我知道标题很混乱所以我会尽力在这里解释...

我有

class T{
   int id;
   string value;
}


List<T> objs;

在我的代码中,当我检索初始列表时,我使用

获取了第一个 ID
int currentID = objs.FirstOrDefault().id;

现在我需要抓住下一个。不知道我当前所在的位置如何获取下一个项目 ID...

 objs.select(x => x.id)
        .where(//you are the object that exists after the one with currentID);

如果你想在某个元素之后获取下一个元素,那么你可以使用 SkipWhile()Skip() 这样的方法:

objs.SkipWhile(x => x.id == currentID).Skip(1).FirstOrDefault();

你只是在索引,所以使用ElementAt:

int i = 0; //current position
int currentID = objs.ElementAt(i).id;
i++;

循环执行得到下一个等

如果你实际上只想遍历所有元素,只需使用 foreach:

foreach (T obj in objs)
{
   int currentID = obj.id;
}

最后,您可以使用 GetEnumerator 并利用 MoveNext 函数以及 Current 属性。这基本上是使用 foreach 但你自己控制迭代。