LINQ to find array indexes of a value

前端 未结 6 1012
轻奢々
轻奢々 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:14

    .Select has a seldom-used overload that produces an index. You can use it like this:

    str.Select((s, i) => new {i, s})
        .Where(t => t.s == "avg")
        .Select(t => t.i)
        .ToList()
    

    The result will be a list containing 2 and 4.

    Documentation here

提交回复
热议问题