I am using LINQ to parse a large list of strings read from csv files. My code works fine with 100MB file. But unable to go beyond it due to stack overflow exception. I am testing my code with 500MB files, where the count of the strings in the list is around 4 million.(approximately 4 million lines in 500MB csv file)
    public List<Metrics> MetricsParser(DateTime StartDate, TimeSpan StartTime, DateTime EndDate, TimeSpan EndTime,int dateIndex,int timeIndex)
    {
        DateTime sd = StartDate;
        DateTime ed = EndDate;
        TimeSpan st = StartTime;
        TimeSpan et = EndTime;
        StreamReader streamReader;
        List<string> lines = new List<string>();
        try
        {
            streamReader = new StreamReader("file.csv");
            lines.Clear();
            while (!streamReader.EndOfStream)
                lines.Add(streamReader.ReadLine());
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (streamReader != null)
                streamReader.Close();
        }
        IEnumerable<Metrics> parsedFileData = null;
        parsedFileData = from line in lines
                         let log = line.Split(",")
                         where (!(line.StartsWith("#")) & (line.Length > 0))
                         let dateVal = _utility.GetDateTime(dateformatType, log[(int)dateIndex], log[(int)timeIndex])
                         let timeVal = _utility.GetTime(log[(int)timeIndex], timeformatType)
                         where (dateVal >= new DateTime(sd.Year, sd.Month, sd.Day, st.Hours, st.Minutes, st.Seconds)
                                 & dateVal <= new DateTime(ed.Year, ed.Month, ed.Day, et.Hours, et.Minutes, et.Seconds))
                         select new Metrics()
                         {
                             Date = dateVal,
                             Metrics1 = log[(int)Metrics1Index],
                             Metrics2 = (Metrics2Index != null) ? log[(int)Metrics2Index] : "default",
                             Metrics3 = (log[(int)Metrics3Index] == null || log[(int)Metrics3Index] == "") ? "-" : log[(int)Metrics3Index],
                             Metrics4 = (log[(int)Metrics4Index] == null || log[(int)Metrics4Index] == "") ? "-" : log[(int)Metrics4Index],
                             Metrics5 = (log[(int)Metrics5Index] == null || log[(int)Metrics5Index] == "") ? "-" : log[(int)Metrics5Index],
                             Metrics6 = (log[(int)Metrics6Index] == null || log[(int)Metrics6Index] == "") ? "-" : log[(int)Metrics6Index],
                             Metrics7 = (log[(int)Metrics7Index] == null || log[(int)Metrics7Index] == "") ? "-" : log[(int)Metrics7Index],
                             Metrics8 = (log[(int)Metrics8Index] == null || log[(int)Metrics8Index] == "") ? "-" : log[(int)Metrics8Index],
                             Metrics9 = (log[(int)Metrics9Index] == null || log[(int)Metrics9Index] == "") ? "-" : log[(int)Metrics9Index],
                         };
        return parsedFileData.ToList();
    }
Any ideas how to achieve the task with larger data.
i tried like below as per some suggestions but it too couldnt overcome stack overflow exception!
try
{
    streamReader = new StreamReader("file.csv");
    while (!streamReader.EndOfStream)
    {
        var line = streamReader.ReadLine();
        if (!(line.StartsWith("#")) & (line.Length > 0))
        {
            var log = line.Split(",");
            var dateVal = _utility.GetDateTime(dateformatType, log[(int)dateIndex], log[(int)timeIndex]);
            parsedData.Add(
                         new Metrics()
                         {
                             Date = dateVal,
                             Metrics1 = log[(int)Metrics1Index],
                             Metrics2 = (Metrics2Index != null) ? log[(int)Metrics2Index] : "default",
                             Metrics3 = (log[(int)Metrics3Index] == null || log[(int)Metrics3Index] == "") ? "-" : log[(int)Metrics3Index],
                             Metrics4 = (log[(int)Metrics4Index] == null || log[(int)Metrics4Index] == "") ? "-" : log[(int)Metrics4Index],
                             Metrics5 = (log[(int)Metrics5Index] == null || log[(int)Metrics5Index] == "") ? "-" : log[(int)Metrics5Index],
                             Metrics6 = (log[(int)Metrics6Index] == null || log[(int)Metrics6Index] == "") ? "-" : log[(int)Metrics6Index],
                             Metrics7 = (log[(int)Metrics7Index] == null || log[(int)Metrics7Index] == "") ? "-" : log[(int)Metrics7Index],
                             Metrics8 = (log[(int)Metrics8Index] == null || log[(int)Metrics8Index] == "") ? "-" : log[(int)Metrics8Index],
                             Metrics9 = (log[(int)Metrics9Index] == null || log[(int)Metrics9Index] == "") ? "-" : log[(int)Metrics9Index],
                         }
                         );
        }
    }
}
thanks for the ideas!
 
    