首页 > 解决方案 > 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); 

标签: c#linq

解决方案


您可以使用Enumerable.Skip

var nextItem = lstTags.Skip(startIndex).FirstOrDefault(a => a.PlateFormID == plateFormID);

这将过滤掉第一个startIndex元素,然后PlateFormID在过滤后的枚举中找到第一个匹配项。


推荐阅读