1

I would like the following code, writes the result from Linq into a csv file in a generic way, in order to not having to specify each one of the columns. Is it possible?

public void extractExamples()
    {
        using (var db = new LinqDB())
        {
            var query = from q in db.Cars
                          select q;

            foreach (var element in query)
            { 
                // Write to a csv file
            }


        }
    }

As an additional point, I would also want to generate the csv in paralell, using:

query.AsParallel().ForAll(...

But, I imagine that I should control the opening and writing file. Therefore is there any tips how to do it?

  • 3
    "for? write asParalele" this is my comment multi thread. Parallel won't speed up writing. – xdtTransform Jan 07 '20 at 13:05
  • 1
    https://joshclose.github.io/CsvHelper/examples/writing/write-class-objects/ – xdtTransform Jan 07 '20 at 13:06
  • 2
    AsParalell() is for asynchronous programming. xdtTransform is correct that it will not speed up your writing. – Jeff Longo Jan 07 '20 at 13:08
  • 2
    Does this answer your question? [Writing data into CSV file in C#](https://stackoverflow.com/questions/18757097/writing-data-into-csv-file-in-c-sharp) – xdtTransform Jan 07 '20 at 13:08
  • @xdtTransform this helped me a lot with writing the csv, thanks for that but I'm didn't get your and JeffLongo comment about write as Paralell.. could you please explain me that better? –  Jan 07 '20 at 16:00
  • 1
    Easy, Write speed is limited by the device write speed. Just as your speed is limited by your handwritting skill. Having multiple hand trying to write on the same paper will cause some clutter. And won't improve the performance. Parallel for writing is almost the same. It may cause more harm than good. Using Parallel won't speed up the writting speed. – xdtTransform Jan 07 '20 at 16:26
  • That's clear, thank you all for the help. –  Jan 08 '20 at 07:42

0 Answers0