如何获取包含字符串的列表的索引

我有一个List ,我检查它是否包含一个字符串:

 if(list.Contains(tbItem.Text)) 

如果这是真的,我这样做:

 int idx = list.IndexOf(tbItem.Text) 

但是如果我有两个相同的字符串怎么办? 我想得到所有具有此字符串的索引,然后使用foreach循环遍历它。 我怎么能这样做?

假设listList

 IEnumerable allIndices = list.Select((s, i) => new { Str = s, Index = i }) .Where(x => x.Str == tbItem.Text) .Select(x => x.Index); foreach(int matchingIndex in allIndices) { // .... } 

这个怎么样:

 List matchingIndexes = new List(); for(int i=0; i 

或使用linq获取索引

 int[] matchingIndexes = (from current in list.Select((value, index) => new { value, index }) where current.value == tbItem.Text select current.index).ToArray();