Return CSV from Api without putting all database results in string

.net asp.net-core c# entity-framework-core

Question

So what I want to do is have an api that gets a couple of columns from a big database, and returns them as CSV (text). The problem arises if i try to first get them, then build the csv, because the memory fills up quickly (400mb for 800k rows). This is the closest I've gotten to getting it to work, but The format isn't really CSV, It's rows separated by Quotes, and it isn't a single string.

 public IEnumerable<string> getCSV()
    {

        var names = typeof(AddressBook).GetProperties()
                    .Select(property => property.Name)
                    .ToArray();
        yield return string.Join(",", names);
        foreach( string name  in names)

        var query = context.AddressBook.Select(x => string.Join(",", x.Name, x.Surname, x.BirthDate)).AsNoTracking();


        foreach (string row in query)
        {
            yield return row;
        }

    }
1
0
2/3/2020 11:49:38 AM

Popular Answer

  1. Your program shouldn't occupy too much memory. It may through MemoryOutOfBounds exception.
  2. API's are intended for quick response. Your api shouldn't take large time to execute/return the data.
  3. If you have too much data, it is recommended to implement pagination for your api.
0
2/3/2020 11:58:09 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow