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.
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.