How to find a sub-list of two consecutive items in a list using Linq

前端 未结 5 1707
长发绾君心
长发绾君心 2021-01-26 19:56

Suppose that we have a list of strings like

\"A\", \"B\", \"C\", \"D\", \"E\", \"F\"

Now I\'d like to search for a sub-list of

5条回答
  •  萌比男神i
    2021-01-26 20:50

    The most elegant solution is the simplest

    Where((x,i) => (x == "D") && (i != list.Count - 1) && (list[i + 1] == "E")).FirstOrDefault();

提交回复
热议问题