0

这是我的代码:

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 (","); )

我不明白需要;传递这些参数,不是吗?我也不明白为什么我不能拆分字符串。

4

4 回答 4

7

错误 1 ​​和 2:您的 for 循环应该是 foreach 循环:

foreach (string dir in directions)

错误 3:拆分中的 S 应为大写:

var fields = line.Split(',');
于 2013-05-15T14:33:05.757 回答
5

for (string dir in directions)应该foreach (string dir in directions)

编辑添加:

另外,这里:

List<Data> perDirection = info.Where(d => d.Direction = dir) as List<Data>;

您似乎在进行分配而不是进行相等性检查,即

d.Direction = dir应该d.Direction == dir

但是,由于您将字符串与布尔值进行比较,它仍然不起作用。

于 2013-05-15T14:28:49.717 回答
1

您需要替换line.split(",")line.Split(',')(注意大写S)。
(也如其他人指出的那样替换为第 67 行)forforeach

于 2013-05-15T14:31:15.260 回答
1

Error1 & Error2 使用 foreach 而不是 for;

Erorr3 它是拆分而不是拆分。

于 2013-05-15T14:38:52.617 回答