LINQ找到一个值的数组索引 [英] LINQ to find array indexes of a value

查看:296
本文介绍了LINQ找到一个值的数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下字符串数组:

Assuming I have the following string array:

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

它是possbile使用LINQ获得匹配一个字符串索引列表?

Is it possbile to use LINQ to get a list of indexes that match one string?

作为一个例子,我想搜索字符串平均,并得到包含列表

As an example, I would like to search for the string "avg" and get a list containing

2,4

这意味着平均可在STR找到[2]和STR [4]。

meaning that "avg" can be found at str[2] and str[4].

推荐答案

。选择有一个很少使用的过载产生的索引。您可以使用它是这样的:

.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()

其结果将是含有2和4的清单。

The result will be a list containing 2 and 4.

这里文档

这篇关于LINQ找到一个值的数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆