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 7968179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:02:12+00:00 2026-06-04T07:02:12+00:00

I am new to LINQ and I am currently using it to process big

  • 0

I am new to LINQ and I am currently using it to process big datasets in csv format (half a million records). I am using a StreamReader to open the files and implement the IEnumerable<> interface in order to populate the results. Below you can see the main part of the reading code:

IEnumerator<Person> IEnumerable<Person>.GetEnumerator()
{
    using (StreamReader streamReader = new StreamReader(filename)){
        streamReader.ReadLine();
        while (!streamReader.EndOfStream){
            string[] values = streamReader.ReadLine().Split(new char[] { ',' });
            Person p = new Person();
            p.Name = values[0];
            p.Age = Convert.ToInt16(values[1]);
            p.Score = Convert.ToDouble(values[2]);
            p.PlotArea = Convert.ToInt16(values[3]);
            p.ForecastConsumption = Convert.ToDouble(values[4]);
            p.Postcode = values[5];
            p.PropertyType = values[6];
            p.Bedrooms = Convert.ToInt16(values[7]);
            p.Occupancy = Convert.ToInt16(values[8]);

            yield return p;
        }
    }
}

and here is a typical query:

var query = from person in reader
            where person.Score > 36.55 && person.Bedrooms < 3
            select person;

My question is this, every time I want to run a query the StreamReader has to open the file. Is there any way that I can open the file once and run multiple queries?

FYI I am very impressed with LINQ, it takes 1.2 seconds to run the query above. It’s just that I will run a lot of rules for the datasets.

  • 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-04T07:02:13+00:00Added an answer on June 4, 2026 at 7:02 am

    My question is this, every time I want to run a query the StreamReader has to open the file. Is there any way that I can open the file once and run multiple queries?

    Well the simplest way would be to load the whole file into a list, e.g.

    var list = reader.ToList();
    
    // Now run multiple queries over list
    

    Obviously that will take quite a lot of memory, but it’s going to be the simplest way to go. If you want to join multiple queries together, you’ll have to work out exactly what you want to do – the composition model in LINQ is mostly in terms of chaining query operations together rather than building multiple queries from the same source.

    Failing that, if neither the complexity of the “multiple queries in one pass” nor the “load the whole file into memory” works for you, you’re likely to be stuck with loading it multiple times.

    One in-between option which may be more memory efficient would be to read all the lines into memory (so you only perform the disk activity once) but then parse those lines multiple times. That will be much more efficient in terms of IO, but worse in terms of CPU.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Linq-to-SQL. Currently I am binding gridview through linq which query written
Currently I'm using this code to create a custom filter: var fName = new
I'm new to using Linq and just started a side project to learn some
New to Linq to Entities. I am using entity framework and linq. In my
I am new to Linq world and currently exploring it. I am thinking about
I wrote a small algorithm using LINQ to read in a bunch of files
Currently i am using xpath with linq to xml to add elements after a
I am looking into creating a new website using ASP.NET 4.0. I am currently
How can I populate data into GridView using LINQ data source. Currently Im doing
There is a proliferation of new LINQ providers. It is really quite astonishing and

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.