不用循环和递归判断值在数组中的索引

发布时间 2023-08-27 23:03:09作者: 每天进步多一点
 ////数组集合
 string[] str = new string[] { "a", "b", "c", "d", "e", "f", "g" };
 ////要查找的字符串
 string Num = "c";

 ////使用Linq查询,将索引和值查出来,
 ////新建一个匿名类,属性包括 aa bool类型,和 Index 索引
 var tt = str.Select((num, index) => new
 {
    aa = (Num == num),
    Index = index
 });
 ////将得到的输出结果进行判断,查找 aa为true的索引值
 ////最后成功得到它的索引
 int number = tt.Where(n => n.aa == true).Last().Index;