[英]How to find an element in the List from a specific index upto the end?
How can I search for an element in a List of Type starting from a specific element?如何从特定元素开始在类型列表中搜索元素?
I can achieve the same using the for loop as follows:我可以使用 for 循环实现相同的效果,如下所示:
bool found = false;
for(int i=counter+1;i<=lstTags.Count()-1;i++)
{
if (lstTags[i].PlateFormID == plateFormID)
{
found = true;
break;
}
}
However, I want to know if it can be done in a more efficient way through a built-in feature like:但是,我想知道是否可以通过内置功能以更有效的方式完成,例如:
var nextItem=lstTags.FirstOrDefault(a=>a.PlateFormID==plateFormID, startIndex);
You can use Enumerable.Skip
:您可以使用Enumerable.Skip
:
var nextItem = lstTags.Skip(startIndex).FirstOrDefault(a => a.PlateFormID == plateFormID);
This will filter out first startIndex
elements and then find the first matching PlateFormID
in the filtered enumerable.这将过滤掉第一个startIndex
元素,然后在过滤后的枚举中找到第一个匹配的PlateFormID
。
You can use Where((obj,index)=>yourLogic)
您可以使用Where((obj,index)=>yourLogic)
Code:代码:
var nextItem=lstTags.Where((a,index) => a.PlateFormID==plateFormID && index > startIndex ).FirstOrDefault();
EX前任
var datas = new[] { "item1","item2","item3"};
var data = datas.Where((obj,index) => index>1 ).FirstOrDefault(); //item3
PS聚苯乙烯
- Index start with 0索引从 0 开始
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.