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

  • SEARCH
  • Home
  • 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 8411761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:30:23+00:00 2026-06-10T00:30:23+00:00

I am writing an application that works with the file system. When the app

  • 0

I am writing an application that works with the file system. When the app first starts, it runs a quick routine to load the requested files and folders into memory for later (time intensive) processing. (See code below). At this point it gives me a count of how many files are going to be processed, which is important in order to display the progress bar.

Once I have the count and the file data, I need to store the data for later processing (e.g. as a global variable or property or class). The problem is that it is being stored as “var” by necessity since it is using LINQ. When I break and examine the variable, it is being stored as a rather complicated mix of SelectQueryOperator and AnonymousType.

My first thought was to go ahead and loop through the data and convert it to simple data that I can store as a List<>, (e.g. store filename and path) but doing that literally takes minutes – up to 10 minutes or more – to process. I am going to have to loop through all that data later anyway in order to do the processing, and there is no way my users are going sit and wait for a list to be built up first.

How can I store this data so that I can access it later without having to convert it into something else first?

var fileNames = 
from dir in Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
select dir;

var fileContents = from file in fileNames.AsParallel() 
// Use AsOrdered to preserve source ordering 
let extension = Path.GetExtension(file)
let Text = File.ReadAllText(file)
select new { Text, FileName = file }; 
  • 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-10T00:30:24+00:00Added an answer on June 10, 2026 at 12:30 am

    Let’s simplify this a bit, and also make var explicit where we can..

    var fileNames = 
    from dir in Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
    select dir;
    

    This is exactly the same as:

    var fileNames = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories);
    

    Which is exactly the same as:

    IEnumerable<string> fileNames = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
    

    Now for:

    var fileContents = from file in fileNames.AsParallel() 
    // Use AsOrdered to preserve source ordering 
    let extension = Path.GetExtension(file)
    let Text = File.ReadAllText(file)
    select new { Text, FileName = file }; 
    

    Going for a one-line-wonder doesn’t normally help readability, but it will help put our object creation all in one place for the sake of discussion:

    var fileContents = from file in fileNames.AsParallel() 
    select new { Text = File.ReadAllText(file), FileName = Path.GetExtension(file) }; 
    

    Which is a ParallelQuery<T> for an anonymous T. To make this something we can store we need to stop using anonymous classes:

    private class NameAndContents
    {
       public string Text{get;set;}
       public string FileName{get;set;}
    }
    
    ParallelQuery<NameAndContents> fileContents = from file in fileNames.AsParallel() 
    select new NameAndContents{ Text = File.ReadAllText(file), FileName = Path.GetExtension(file) }; 
    

    There’s now nothing stopping you from storing that in a field of type ParallelQuery<NameAndContents>.

    You might want to check on the logic here though in two ways:

    1. The workings of Directory.EnumerateFiles is such that it needs to know the value of a given iteration in order to calculate the next. (It’s based on the FindNextFile Windows API function). This makes it poor at being parallelised. Just how much the inherent waiting involved in ReadAllText balances that out is hard to predict. I’d not only test it against the non-parallel version, but I’d re-test after any changes made because any changes are going to throw off that balance in a new way.

    2. The biggest hit here is that ReadAllText. If it’s at all possible to replace that with something that makes use of the text in a more on-demand way, then it could be a big win.

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

Sidebar

Related Questions

I'm writing an application that uses TwainDotNet for scanning. Everything works fine, but scanning
I am writing an application in C# that reads info from a .mdb file
I'm writing an application that manages the hosts files entries. So I wrote a
I'm writing a Rails application that serves files stored on a remote server to
I've been writing an application that monitors certain directory for files being added, and
I'm writing a small application which works at compiling a file from a source
Im writing an application that supposed to send coordinates in an SMS, but I've
I am writing an application that allows a user to place images on a
I am writing an application that will allow an android phone and java application
I am writing an application that sends commands to a unix shell. I am

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.