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

                ///////////////////////////////////////////////////////数组集合
                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;


原文转载:http://hi.baidu.com/xiaowei0705/blog/item/bfacf9cda621b12bf9dc61f4.html
posted on 2011-04-13 16:40  xiaowei0705  阅读(361)  评论(0编辑  收藏  举报