簡體   English   中英

如何將字符串列表轉換為自定義數據類型?

[英]How to cast string list into a custom datatype?

背景:我正在嘗試從包含字符串和雙精度字符的csv文件讀取。 我想保留字符串以用於搜索和索引目的,但能夠對列表中的double值執行數據分析。 我也不想在將整個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++;

        }

    }

好吧-我越看越多,看起來我需要的是一組DataTable元素。 我沒有列出列表,而是為每個變量類型設置了一個數據表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM