I have to write a large data (12L)records in a csv file.I am using Streamwriter, to write in file but after around 6L records it gives me outofmemory error.
also I am writing this file on server directly, is there any way I can than download this on browser later?
This is my code: searchResult.Entities=is a list of EntityObject
using (StreamWriter fs = new StreamWriter
(new FileStream("Temp.csv", FileMode.Create)))
                    {
                        AddText(fs, headerString.ToString());
                        foreach (var org in searchResult.Entities.ToList())
                        {
                            StringBuilder sb = new StringBuilder();
                            appendText(sb, org.Id);
                            appendText(sb, org.LeadNo.HasValue ? org.LeadNo.Value.ToString() : "");
                            fs.WriteLine(sb.ToString());
                           foreach (var activity in org.Activities)
                           {
                              var activityString = new StringBuilder();
                             activityString.Append(" , , , , , , , , , , , , , , , , , ,");
                               appendText(activityString, org.Id);
                                appendText(activityString, org.LeadNo.HasValue ? org.LeadNo.Value.ToString() : "");
                            fs.WriteLine(activityString.ToString());
                           }
                        }
Getting all results from database int the object only take 100ms, but writing in file is around 1minute for 6L records.
 
    