日期:2014-05-20 浏览次数:21427 次
int[] NNN = { 11, 22, 33, 44 }; int sort = NNN.Select((s, index) => new { s, index }).FirstOrDefault(w => w.s == 22).index; int sort1 = NNN.ToList().IndexOf(22);
------解决方案--------------------
void Main() { var NNN = new int[4] { 11, 22, 33, 44 }; var newNNN = NNN.ToDictionary(m => m, n1 => NNN.Select((a, index) => new { a, index }).FirstOrDefault(b =>b.a== n1).index); Console.WriteLine(newNNN[22]); //1 }
------解决方案--------------------
int[] NNN = { 11, 22, 33, 44 }; var query = NNN.Where(n => n == 22).Select((n, i) => new { n = n, i = i+1 }); query.ToList().ForEach(q => Console.WriteLine("n={0} i={1}", q.n, q.i));
------解决方案--------------------
第一个问题,用Array.IndexOf,远简洁于Linq
int[] aa = {1,2,3,4,5}; int index = Array.IndexOf(aa, 1);