如何将字符串列表强制转换为自定义数据类型

本文关键字:转换 定义数据类型 字符串 列表 | 更新日期: 2023-09-27 18:35:34

>背景:我正在尝试从包含字符串和双精度混合的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++;
        }
    }

如何将字符串列表强制转换为自定义数据类型

好吧 - 我看得越多,看起来一组 DataTable 元素就是我需要的。 我没有做列表列表,我只是为每个变量类型设置一个数据表。