如何从特定元素开始搜索类型列表中的元素?
我可以使用 for 循环实现相同的效果,如下所示:
bool found = false;
for(int i=counter+1;i<=lstTags.Count()-1;i++)
{
if (lstTags[i].PlateFormID == plateFormID)
{
found = true;
break;
}
}
但是,我想知道是否可以通过内置功能以更有效的方式完成它,例如:
var nextItem=lstTags.FirstOrDefault(a=>a.PlateFormID==plateFormID, startIndex);
最佳答案
您可以使用 Enumerable.Skip
:
var nextItem = lstTags.Skip(startIndex).FirstOrDefault(a => a.PlateFormID == plateFormID);
这将首先过滤掉 startIndex
元素,然后在过滤后的枚举中找到第一个匹配的 PlateFormID
。
关于c# - 如何从特定索引到末尾查找列表中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54749529/