2015-05-27 100 views
0

enter image description here如何从C#中的列表中获取索引值?

我有以下的是我得到例如 index[0] value[0]"example"[1]"example" [2]"example"我想在一个时间

foreach (var pair in profession.Professions.Select((x, i) => new { Index = i, Value = x })) 
{ 
    Console.WriteLine(pair.Index + ": " + pair.Value); 
} 

例如只访问一个索引的所有值的指数值码:当用户将首先选择在列表框中的项目和该项目索引是[0]和值是[0]1 [1]480[2]749[3]270我想要显示在messagebox中的所有值。

+3

“一次只有一个索引全部值”没有意义。你能重新表达吗? – Richard

+1

你能否澄清你的问题?它并没有解释你想要达到的目标。 – Alaminut

+0

理查德先生我的意思是当用户将点击列表框中的项目时,我会找到该项目索引并获取该特定索引的所有值 – user

回答

0

因为你的代码是只是局部的不是很清楚,我给这个基于您提供的图片上一拍:

//Defining the structure like in your example 
static List<int[]> rootArray = new List<int[]>{ 
     new int[]{1,5,7,10}, 
     new int[]{2,4,6,8}, 
     new int[]{3,6,9,12} 
    }; 

static void Main(string[] args) 
    { 
     //Same loop you posted in your OP, only added a .Where after the initial .Select just to get elements with Index == 0 
     foreach(var p in rootArray.Select((x,i) => new {Index = i, Value = x}).Where(f => f.Index == 0).Select(x => x.Value)) 
     { 
      //Since this is a List<int[]> your values are int arrays hence you need a loop to display all the values in it. 
      foreach(var i in p) 
      { 
       Console.WriteLine(i); 
      } 
     } 

     Console.ReadLine(); 
    } 

希望这有助于。