在c#中获取列表中重复项的索引

Rya*_*yan 4 c# list

我正在寻找一种方法来获取列表中的关键字搜索列表中的所有元素的索引.例如,我的列表有:

Hello World
Programming Rocks
Hello
Hello World
I love C#
Hello
Run Code Online (Sandbox Code Playgroud)

现在从这个字符串列表中,我想获得所有表示Hello World的元素索引.我尝试了以下但它只返回它找到的具有我的搜索条件的第一个索引:

    for (int i = 0; i< searchInList.Count; i++)
        foundHelloWorld[i] = searchInList.IndexOf("Hello World");
Run Code Online (Sandbox Code Playgroud)

有人知道这样做的方法吗?

谢谢

Yur*_*ich 8

searchInList.Select((value, index) => new {value, index})
    .Where(a => string.Equals(a.value, "Hello World"))
    .Select(a => a.index)
Run Code Online (Sandbox Code Playgroud)

如果您正在尝试搜索的不仅仅是"Hello World",您可以这样做

searchInList.Select((value, index) => new {value, index})
    .Where(a => stringsToSearchFor.Any(s => string.Equals(a.value, s)))
    .Select(a => a.index)
Run Code Online (Sandbox Code Playgroud)