C# 如何选择数据结构
我有一个这样的数据表 我想对它执行许多操作,比如C# 如何选择数据结构,c#,data-structures,C#,Data Structures,我有一个这样的数据表 我想对它执行许多操作,比如 当天气晴朗时,有多少次玩“不”呢 有多少次WINDY是“真的”而剧本是“是的” 我应该使用什么数据结构? 现在,我有一个简单的数组,这是一个很难控制的任务 string[,] trainingData = new string[noOfRecords,5]; using (TextReader reader = File.OpenText("input.txt")) { int i =
- 当天气晴朗时,有多少次玩“不”呢
- 有多少次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++;
}
}
string[,]trainingData=新字符串[noOfRecords,5];
使用(TextReader=File.OpenText(“input.txt”))
{
int i=0;
字符串文本=”;
而((text=reader.ReadLine())!=null)
{
字符串[]位=text.Split(“”);
对于(int j=0;j
这是一个非常令人不安的问题,因为这是一个关于观点的问题,而不是一个有效的编程问题。很多程序员会这样或那样说。对于您正在显示的表,使用数组并没有问题,就像您在前面提到的简单查询中所做的那样。对于更复杂的数据和查询,我建议您花点时间和精力研究创建一个类并将值写入属性。即:
public class Weather
{
public string Outlook {get;set;}
...
}
然后将它们存储到
列表中
集合中(在循环过程中)。如前所述,您可以对其运行LINQ
查询。互联网上充满了如何使用LINQ
扩大@Doan 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>();
}
static void Main(字符串[]args)
{
Table Table=新表();
int count1=table.records.Where(r=>r.Play==false&&r.Outlook.ToLower()==“sunny”).Count();
}
公开课记录
{
公共布尔游戏;
公共字符串展望;
}
公共类表
{
//这应该是私有的,表应该是IEnumable
公共列表记录=新列表();
}
使用IEnumerable
如何?然后您可以使用LinQ
对其执行条件选择?我正在查看更简单的行,例如ArrayList或Generics。我认为,您应该为表创建一个类,并使用表列作为其属性。表中的每一行都对应于该类的一个实例。然后将这些对象放入列表中。现在您可以轻松地对该列表执行条件选择了嗯,您的代码中有一个小错误,=
而不是=
:谢谢,我编写了一个编译示例