Getting index of duplicate items in a list in c#

后端 未结 4 1397
星月不相逢
星月不相逢 2021-01-18 21:10

I am looking for a way to get the index of all elements in a list from a keyword search within the list. So for example my list has:

Hello World
Programming          


        
4条回答
  •  抹茶落季
    2021-01-18 21:45

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

    If you're trying to search for more than just "Hello World", you could do

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

提交回复
热议问题