LINQ列表中的下一项

LINQ列表中的下一项

问题描述:

看看我的问题 HERE ,我现在想返回下一个推荐对象(位于符合条件的对象之后).

Taking a look at my question HERE, I now want to return the next recommendation object (after) the one that matches the criteria.

所以说我在10中找到第6项,我希望查询返回第7项.

So say I found item 6 out of 10, I'd like the query to return item 7 instead.

还是有更好的方法?

由于有List<T>对象,因此可以使用其FindIndex方法而不是Where来获取第一个匹配项的索引,而不是项目本身:

Since you have a List<T> object you can use its FindIndex method instead of Where to get the index of the first matching item rather than the item itself:

int index = recommendations.FindIndex(rp =>
                                            rp.Products.Any(p => p.Product.Code == "A") 
                                         && rp.Products.Any(p => p.Product.Code == "B")
                                      );

有了索引后,您就可以获取下一个或上一个项目或任何您想要的内容.

Once you have the index you can get the next item or previous item or whatever you want.