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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:55:46+00:00 2026-05-20T13:55:46+00:00

What is the optimal way to write the Values available in a List to

  • 0

What is the optimal way to write the “Values” available in a List to a Text file? The catch is the Text file’s name should be determined by the Key.

For example, I managed to construct a List like this:

["Animal", "Lion|Roars"]
["Animal", "Tiger|Roars"]
["Bird",   "Eagle|Flies"]
["Bird",   "Parrot|Mimics"]

We need to write two files based on the above: Animal.txt and Bird.txt each containing their respective values only.

What is an efficient way to do this?

Thank you SOF community.

  • 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-05-20T13:55:46+00:00Added an answer on May 20, 2026 at 1:55 pm

    You do not need to try to linqify everything. When you create a grouping you create from all data in the list a dictionary before you can write a single line to the output. This will consume at least twice as much memory as it is necessary.
    This design eliminates lazy processing since you are eagerly reading everything into memory before you can write output.

    Instead you can process the list one by one and write to the current line to the right file. This can be as easy as a hash table lookup for the right file stream by using Animal or Bird as keys to choose the right output file.

    static Dictionary<string, StreamWriter> _FileMap = new Dictionary<string, StreamWriter>();
    
    static void Main(string[] args)
    {
        var data = new List<KeyValuePair<string, string>>
        {
            new KeyValuePair<string, string>("Animal", "Lion|Roars"),
            new KeyValuePair<string, string>("Animal", "Tiger|Roars"),
            new KeyValuePair<string, string>("Bird", "Eagle|Flies"),
            new KeyValuePair<string, string>("Bird", "Parrot|Mimics")
        };
    
        foreach (var line in data) // write data to right output file
        {
            WriteLine(line.Key, line.Value);
        }
    
        foreach (var stream in _FileMap) // close all open files
        {
            stream.Value.Close();
        }
    }
    
    static void WriteLine(string key, string line)
    {
        StreamWriter writer = null;
        if (false == _FileMap.TryGetValue(key, out writer))
        {
            // Create file if it was not opened already
            writer = new StreamWriter(File.Create(key+".txt"));
            _FileMap.Add(key,writer);
        }
        writer.WriteLine(line);  // write dynamically to the right output file depending on passed key
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been thinking about the optimal way to create an XML file using
What is the optimal way to write the code which interacts with DB using
I need to decide on the optimal way to write a C# client application
What would be the optimal way to develop a basic graphical application for Windows
I was told that the optimal way to program in C++ is to use
I realize that parameterized SQL queries is the optimal way to sanitize user input
I am trying to find out if there is a more optimal way for
There's a common way to store multiple values in one variable, by using a
Whats the fastest or perhaps optimal way to read elements, node, attributes from a
What is the most optimal way to find repetition in a infinite sequence of

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.