Linq-获取数组的最后一个非零数字的索引 [英] Linq - Get the Index of the Last Non-Zero Number of Array
本文介绍了Linq-获取数组的最后一个非零数字的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一个Linq表达式返回数组中最后一个非零值的索引?我对扩展名不感兴趣,只对一个简单的linq表达式感兴趣.
Is there a Linq expression that returns the index of the last non-zero value in an array? I'm not interested in an extension, only a simple linq expression.
我正在想像这样的伪代码:
I'm imagining something like this pseudo code:
int index = {0, 2, 1}.LastOrDefaultAt(i => i > 0);
返回值应为2;
推荐答案
You can use the Array.FindLastIndex<T>
method for this:
int index = Array.FindLastIndex(myIntArray, item => item > 0);
我注意到您在问题文本中提到的是非零"而不是大于零".您的谓词应为:item => item != 0
吗?
I notice that you mention "non-zero" rather than "greater than zero" in your question text. Should your predicate be: item => item != 0
?
这篇关于Linq-获取数组的最后一个非零数字的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文