c# - 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:
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);
解决方案
您可以使用Enumerable.Skip
:
var nextItem = lstTags.Skip(startIndex).FirstOrDefault(a => a.PlateFormID == plateFormID);
这将过滤掉第一个startIndex
元素,然后PlateFormID
在过滤后的枚举中找到第一个匹配项。
推荐阅读
- php - laravel 6 mix 中的错误,返回页面不起作用
- odoo - 参考字段不会在 Odoo 的前端呈现
- python - super() 类继承,钻石问题
- python-3.x - 使用 python 以自定义帧速率播放视频
- java - 如何将 int 从方法传递给 RecyclerAdapter 中的 onBindViewHolder 方法
- c++ - 在 TinyXML 中检查元素是否存在
- c++ - 当另一个数组是 memset 时,memset 是否会更改数组长度?
- java - 创建名为“flywayInitializer”的 bean 时出错
- php - “关于作者”字段中批准的电子邮件显示代码(wordpress)
- python - 我在制作 pyautogui 程序时遇到了 windows 错误和 os 错误