查找匹配使用LINQ的条件元素的第一个索引 [英] Finding first index of element that matches a condition using LINQ
问题描述
var item = list.Where(t => somecondition);
我希望能够找出返回,实际上元素的索引,在我的情况下,我要的是一个索引,这样我就可以.Skip()到列表中那么多。
I would love to be able to find out the index of the element that was returned, in fact, in my case all I want is an index, so that I can .Skip() into the list by that much.
有没有办法在一个IEnumerable做到这一点?我讨厌使用列表< T>
这一点,但确实有一个 FindIndex()
方法
Is there a way to do this in an IEnumerable? I'd hate to use a List<T>
for this, but that does have a FindIndex()
method
推荐答案
我需要更多的上下文,但如果你只是得到一个索引,使您可以拨打 .Skip
,我会建议采取看看 .SkipWhile
。
I'd need more context, but if you're just getting an index so that you can call .Skip
, I would recommend taking a look at .SkipWhile
.
如果你真正需要的指数,我建议编写自己的 .IndexOf
扩展方法。
If you do really need the index, I'd suggest writing your own .IndexOf
extension method.
这篇关于查找匹配使用LINQ的条件元素的第一个索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!