LINQ列表中的下一项 [英] LINQ Next Item in List
本文介绍了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.
这篇关于LINQ列表中的下一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文