LINQ to find array indexes of a value

前端 未结 6 1072
轻奢々
轻奢々 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条回答
  •  萌比男神i
    2021-02-01 00:55

    You need a combined select and where operator, comparing to accepted answer this will be cheaper, since won't require intermediate objects:

    public static IEnumerable SelectWhere(this IEnumerable source, Func filter, Func selector)
            {
                int index = -1;
                foreach (var s in source)
                {
                    checked{ ++index; }
                    if (filter(s))
                        yield return selector(s, index);
                }
            }
    

提交回复
热议问题