2013-07-17 69 views
3

我有一个这样的数据表。如何选择数据结构

table of data

我要像

  • 多少次是PLAY对其执行许多操作“不”时,它是阳光明媚
  • 有多少次是WINDY“真”时发挥得'是'

我应该使用什么数据结构? 现在我有一个简单的数组,它正在成为控制任务中的一员。

string[,] trainingData = new string[noOfRecords,5]; 
     using (TextReader reader = File.OpenText("input.txt")) 
     { 
      int i =0; 
      string text = ""; 
      while ((text = reader.ReadLine()) != null) 
      { 
       string[] bits = text.Split(' '); 
       for (int j = 0; j < noOfColumnsOfData; j++) 
       { 
        trainingData[i, j] = bits[j]; 
       } 
       i++; 
      } 
     } 
+3

怎么回合使用'IEnumerable'那么你可以使用'LinQ'执行它,条件选择? –

+1

我正在寻找简单的线条,例如ArrayList或泛型 –

+1

在我看来,您应该为您的表创建一个类,并使用表列作为它的属性。表中的每一行都将对应该类的一个实例。然后将这些对象放入一个列表中。现在你可以很容易地在该列表上执行条件选择 –

回答

3

为了拓宽@Dan cuong的口气, 我会使用一个令人鼓舞的物品列表。每个对象都可以调用 :Record和集合可以调用Table。 (表是IEnumarable)。

下面是一个简单的例子:

static void Main(string[] args) 
     { 
      Table table = new Table(); 
      int count1 = table.records.Where(r => r.Play == false && r.Outlook.ToLower() == "sunny").Count(); 
     } 

     public class Record 
     { 
      public bool Play; 
      public string Outlook; 
     } 


     public class Table 
     { 
      //This should be private and Table should be IEnumarable 
      public List<Record> records = new List<Record>(); 

     } 
+1

uhmm,在你的代码中有一个小错误,'=='而不是'=':D –

+0

谢谢,我写了一个编译的例子 –

+0

好的镜头,bro:D –

1

这是一个非常不舒服的问题,因为它是关于意见而不是有效的编程问题。很多程序员会说这个或那个。对于你正在显示的表格,使用数组没有问题,就像你刚刚提到的那样用于简单查询。对于更复杂的数据和查询,我建议您花费时间和精力学习LINQ

1

创建一个类并将值写入属性。 I.e .:

public class Weather 
{ 
public string Outlook {get;set;} 
... 
} 

然后将它们存储到List<Weather>集合中(在您的循环中)。就像已经说过的那样,您可以在其上运行LINQ查询。互联网充满了如何使用LINQ的例子。