这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
namespace DataAnalysis
{
class Data
{
public int InTime;
public string InLocation;
public bool Direction;
public int LOS_F;
// create a Data object from a CSV format string.
static Data FromString(string line)
{
var fields = line.split(",");
return new Data
{
InTime = TimeSpan.Parse(fields[3]),
InLocation = fields[5],
Direction = fields[5][0], // to get the direction E/N/S/W
LOS_F = float.Parse(fields[16])
};
}
}
class Program
{
string[] directions = new string[] { "E", "N", "S", "W" };
static void Main(string[] args)
{
var path = @"C:\Documents and Settings\Siva-Admin\Desktop\5.5 Capacity Models\";
// ^--- No need to escape the backslashes
var subdirs = Directory.GetDirectories(path);
// The subdirs variable contains the FULL paths
foreach (string subdir in subdirs)
{
List<List<float>> allAvgs = new List<List<float>>();
using (StreamWriter compiled = new StreamWriter(
Path.Combine(subdir, "compiledresults.csv")))
{
compiled.Write("heading,EastAvg,NorthAvg,SouthAvg,WestAvg");
for (int i = 1; i <= 10; i++)
{
List<Data> info = new List<Data>();
using (StreamReader reader = new StreamReader(
Path.Combine(subdir, "results" + i.ToString() + @"\JourneyTimes.csv")))
{
// Read the header line first!
string line = reader.ReadLine();
while ((line = reader.ReadLine()) != null)
info.Add(Data.FromString(line));
}
List<float> avgs = new List<float>();
for (string dir in directions)
{
List<Data> perDirection = info.Where(d => d.Direction = dir) as List<Data>;
float sum = perDirection.Sum(d => d.LOS_F);
float average = sum / perDirection.Count();
avgs.Add(average);
}
allAvgs.Add(avgs);
compiled.Write("results" + i.ToString() + "," + string.Join(",", avgs) + "\n");
}
compiled.Write("scenario_average");
for (int j = 1; j <= 4; j++)
{
compiled.Write("," + allAvgs.Sum(d => d[0]) / allAvgs.Count());
}
}
}
}
}
}
我收到以下错误:
Error 1; expected(Line 67, "for (string dir in directions)" )
Error 2; expected(LINE 67, " " )
Error 3 'string' does not contain a definition for 'split' and no extension method 'split' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?) (LINE 20, var fields = line.split (","); )
我不明白需要;
传递这些参数,不是吗?我也不明白为什么我不能拆分字符串。