Very simple C# CSV reader

后端 未结 7 1483
渐次进展
渐次进展 2020-11-27 15:08

I\'d like to create an array from a CSV file.

This is about as simple as you can imagine, the CSV file will only ever have one line and these values:



        
7条回答
  •  余生分开走
    2020-11-27 15:58

    You can try the some thing like the below LINQ snippet.

    string[] allLines = File.ReadAllLines(@"E:\Temp\data.csv");
    
        var query = from line in allLines
                    let data = line.Split(',')
                    select new
                    {
                        Device = data[0],
                        SignalStrength = data[1],
                        Location = data[2], 
                        Time = data[3],
                        Age = Convert.ToInt16(data[4])
                    };
    

    UPDATE: Over a period of time, things evolved. As of now, I would prefer to use this library http://www.aspnetperformance.com/post/LINQ-to-CSV-library.aspx

提交回复
热议问题