2015-01-06 25 views
0

所以不得不定义为String[][] data_set_examples 2D阵列保持以下数据:滤波行

Sunny,Hot,High,Weak,No 
Sunny,Hot,High,Strong,No 
Overcast,Hot,High,Weak,Yes 
Rain,Mild,High,Weak,Yes 
Rain,Cool,Normal,Weak,Yes 
... 

我想通过一个特定的值,以过滤行,对于包含例如行“热”(列索引1)

我明白,一种可能性可能是通过使用LINQ。虽然我不熟悉,但我尝试了以下方法,但是没有进行过滤。

var result = from u in data_set_examples 
         where u[column_index].Equals(attribute_value) 
         select u; 

我做错了什么?有没有其他方法?

+0

您的代码看起来不错。你确定'column_index'是正确的吗?你有没有试过设置一个断点并检查'data_set_examples'中的数据以确保它是你的想法? –

+0

您如何证明这一点:“但是没有进行过滤”? – dotctor

+1

为什么不只是创建一个类,它包含数据,当前是矩阵行,并创建一个List或任何IEnumerable?这绝对是反面向对象的。这将是更容易过滤,转换等。 –

回答

1

您的代码看起来不错,我认为问题在于您检查过滤结果的方式。

当你使用

foreach (string[] s in result) 
{ 
    Console.WriteLine(s); 
} 

你只是写的string[]

类型名称,但是你应该那些string[]这是你的结果里看到里面(string[][]

你可以做到这一点双向

foreach (string[] s in result) 
{ 
    //concatenate all the values in s 
    Console.WriteLine(string.Join(",", s)); 
} 

foreach (string[] s in result) 
{ 
    //iterate through strings in s and print them 
    foreach (string s1 in s) 
    { 
     Console.Write(s1 + " "); 
    } 
    Console.WriteLine(); 
} 
+1

好吧,非常感谢您指出。这是问题所在。有效。 – User49230

1

我曾经尝试这样做,只是证实了它的工作原理:

string[][] data_set_examples = new string[][]{ 
       new string[]{"Sunny", "Hot", "High", "Weak", "No"}, 
       new string[]{"Sunny", "Hot", "High", "Strong", "No"}, 
       new string[]{"Overcast", "Hot", "High", "Weak", "Yes"}, 
       new string[]{"Rain", "Mild", "High", "Weak", "Yes"}, 
       new string[]{"Rain", "Cool", "Normal", "Weak", "Yes"}, 
      }; 
      IEnumerable<string[]> result = from u in data_set_examples 
         where u[1].Equals("Hot") 
         select u; 
      foreach (string[] s in result) { 
       foreach (string part in s) 
        Console.Write(part + " "); 
       Console.WriteLine(); 
      } 
      Console.Read(); 

产生输出:

Sunny Hot High Weak No 
Sunny Hot High Strong No 
Overcast Hot High Weak Yes