【问题标题】:How to cast string list into a custom datatype?如何将字符串列表转换为自定义数据类型?
【发布时间】:2013-10-16 16:27:15
【问题描述】:

背景:我正在尝试从包含字符串和双打混合的 csv 文件中读取。我想将字符串保留原样用于搜索和索引目的,但能够对列表中的双精度值执行数据分析。我也不想在将整个 csv 文件分成不同的类别之前将其读入内存,因此我尝试将其分配给具有自定义数据类型的列表,因为它是从 csv 读取的。

如何将单个列表条目转换为双精度然后将其添加到列表中?

class data
{
    public List<string> timefrom { get; set; }
    public List<string> timeto { get; set; }
    public List<string> type { get; set; }        
    public List<string> site { get; set; }
    public List<string> box { get; set; }
    public List<string> group { get; set; }        
    public List<string> sourcecopy { get; set; }
    public List<string> destcopy { get; set; }
    public List<string> gridid { get; set; }        
    public List<string> stat { get; set; }
    public List<double> value { get; set; }
    public List<string> unit { get; set; }
    public List<string> peak { get; set; }
}

class Class1
{
    public List<data> 
        WANtpfromSite, 
        WANtpbyCG, 
        IncomingWritesbyCG, 
        LinkUtilbyCG, 
        BoxUtil, 
        CompressionCPU, 
        ReplicationCPU, 
        CompressionRatio, 
        DeduplicationRatio, 
        IncomingWritesbyBox, 
        IncomingWritesbyBoxReplicating, 
        IObyBox, 
        IObyBoxReplicating, 
        PacketLoss, 
        LineLatency;


    public void getDayData(string directory)
    {

        string[] fileEntries;

        fileEntries = System.IO.Directory.GetFiles(directory + "/home/www/info/long_term_stats");
        string filePath = fileEntries[0];
        System.IO.StreamReader sr = new System.IO.StreamReader(filePath);
        List<string> Line;
        int row = 1;
        while (!sr.EndOfStream)
        {
            //Splits the line read into a list of string values
            Line = sr.ReadLine().Split(',').ToList<string>();

            // Here I'll probably use a switch case to set the variable, but this is an example of what it should do.
            if (Line[8] == "Wan Throughput from site")
            {
                //Here is where I'm having the problem - it can't implicitely convert the string list to a data list because
                // of that single double value within the list.  How would I go about explicitely converting the "Line" 
                //variable to a <data> type?

                WANtpfromSite = List < data > datatype;
            }
            row++;

        }

    }

【问题讨论】:

  • 如果有允许这样做的数据类型,或者我不知道的免费库,请告诉我!我对 C# 还很陌生(在 MATLAB 上咬牙切齿,所有它都是易于转换的数据类型的荣耀 - 所以这类问题对我来说是新的)

标签: c# list csv types


【解决方案1】:

嗯 - 我越看越像一组 DataTable 元素的东西就是我需要的。我没有做一个列表列表,而是为每个变量类型设置了一个数据表。

【讨论】:

    猜你喜欢
    • 2020-03-17
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    • 2013-11-27
    • 2019-11-22
    • 2017-06-22
    • 2021-05-01
    • 1970-01-01
    相关资源
    最近更新 更多