c# - Returning a large collection serialized as JSON via MVC controller -


i have large result set want return ajax call using json.

i started creating collection of objects , serialize collection collection creation throw system.outofmemoryexception.

i've tried change implementation stream json without having collection still system.outofmemoryexception.

here current code snippets.

  using (var stream = new memorystream())   {       using (var streamwriter = new streamwriter(stream))       {           using (var jsonwriter = new jsontextwriter(streamwriter))           {               var serializer = new jsonserializer();                serializer.serialize(jsonwriter,new { pins = makepins(model), missinglocations = 0 });                jsonwriter.flush();           }       }    stream.seek(0, seekorigin.begin);    return new filestreamresult(stream, "application/json"); 

the makepins function looks this:

var pindata = _geographyservice.getenumerationqueryable()                 .selectmany(x => x.enumeratedpersonrolecollection)                 .applyfilter(model).where(x => x.enumerationcentre.location != null)                 .asnotracking()                 .asenumerable();  return pindata.select(item => new mappin     {         id = item.enumerationcentre.enumerationcentreuid.tostring(),         name = item.person.fullname,         fillcolour = getmappincolour(item, model),         latitude = item.enumerationcentre.location.latitude,         longitude = item.enumerationcentre.location.longitude,         count = item.issuedvouchercollection.count()     }); 

i've tried using yield return instead of select outofmemoryexception throw withing select function.

i've done fair bit of googling can't quite see else try.

your current solution still have same problem, because before return collect , store data in memory stream

you can try in following fashion:

public actionresult robotstext() {     response.contenttype = "application/json";      response.write("[");      foreach(var item in items)     {          response.write(jsonserializer.serialize(item));          if ( /*not last*/)         {             response.write(",");         }     }      response.write("]");         return new emptyresult(); } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -