LINQ to find array indexes of a value

前端 未结 6 1008
轻奢々
轻奢々 2021-02-01 00:23

Assuming I have the following string array:

string[] str = new string[] {\"max\", \"min\", \"avg\", \"max\", \"avg\", \"min\"}

Is it possbile t

6条回答
  •  [愿得一人]
    2021-02-01 01:01

    While you could use a combination of Select and Where, this is likely a good candidate for making your own function:

    public static IEnumerable Indexes(IEnumerable source, T itemToFind)
    {
        if (source == null)
            throw new ArgumentNullException("source");
    
        int i = 0;
        foreach (T item in source)
        {
            if (object.Equals(itemToFind, item))
            {
                yield return i;
            }
    
            i++;
        }
    }
    

提交回复
热议问题