Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9147567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:01:14+00:00 2026-06-17T11:01:14+00:00

I am using LINQ to parse a large list of strings read from csv

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:01:15+00:00Added an answer on June 17, 2026 at 11:01 am

    Try to parse file line by line, instead of saving in to memory, like this

    var parsedFileData = new List<Metrics>();
    
    while (!streamReader.EndOfStream)
    {
        var line = streamReader.ReadLine();
    
        if(IsLineNeedToBeParsed(line))
            parsedFileData.Add(ParseLine(line));
    } 
    

    Where ParseLine is the method, which has content of your LINQ query, but operates on single line and IsLineNeedToBeParsed is your where clause.. As I’ve noticed – you don’t do any joining of lines.

    Avoid loading whole file contents and then executing some large query with lots of let clauses – it will consume lots of memory during execution.

    Try to create pure functions that filter, select of aggregate data and then, if you still don’t like performance, try to optimize query by adding state, eliminating redundant computation, maybe caching, adding batches and so on.

    One quick point to make: you should make file loading lazy, like this:

    private IEnumerable<string> GetAllLines(string path)
    {
        using (StreamReader streamReader = new StreamReader(path))
        {
            while (!streamReader.EndOfStream)
            {
                yield return streamReader.ReadLine();
            } 
        }
    }
    

    Then you can call it from LINQ query like

    from line in GetAllLines("file.csv")
    

    and all lines will be loaded on demand and your memory consumption should be relatively constant during execution.

    UPDATE:

    I’ve just found, that File.ReadLines(string path) reads file lazily by creating ReadLinesIterator internally . So you can use this call just inside your LINQ query.

    I’ve took some courage to refactor your code a little bit. Note that you still need to add some check and this is not the final version – I just want to show general idea. Also note, that I haven’t compile it – because you have access to parser state and I know nothing of it’s type and values. Code is a little bit longer than yours, but I never forget Clean Code book by Robert Martin, which has big point of “It’s not the shortness, that makes code readable”. Please correct me if I’m wrong somewhere.

    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;
        List<Metrics> parsedFileData = new List<Metrics>();
    
        using (StreamReader streamReader = new StreamReader("file.csv"))
        {
            while (!streamReader.EndOfStream)
            {
                var line = streamReader.ReadLine();
    
                if(IsLineNeedToBeParsed(line))
                    parsedFileData.Add(ParseLine(line));
            } 
        }
    
        return parsedFileData;
    }
    
    private bool IsLineNeedToBeParsed(string line)
    {
        return !(line.StartsWith("#")) && (line.Length > 0) && IsInDateRange(line);
    }
    
    private bool IsInDateRange(string line)
    {
        var dateVal = GetDateTime(line);
        return 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);
    }
    
    private Metrics ParseLine(string line)
    {
        var log = line.Split(',');
        var time = _utility.GetTime(log[(int)timeIndex], timeformatType);
        var dateVal = GetDateTime(line);
        return new Metrics{  /* fill values here */ }
    }
    
    private string[] GetDateTime(string line)
    {
        var log = line.Split(',');
        return _utility.GetDateTime(dateformatType, log[(int)dateIndex], log[(int)timeIndex]);
    }
    
    public class Metrics{}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to parse some large XML file using linq to XML but I
I'm using XDocument and LINQ to parse some XML files, and some worked fine,
I'm trying to parse out an complex XML file using LINQ. The files contains
Using LINQ to Entities, how can I determine if any item from a List
I'm using the following code to load a list of objects from XML using
I'm using Linq to parse a simple XML file into a var (ienumerable), I'm
I parse an xml results from a webservice using linq : XElement items =
I wrote a small algorithm using LINQ to read in a bunch of files
I want to read a xml file using Linq. This xml file is composed
It is working fine for me to read excel 2007 using LinQ and openXML:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.