Self referencing loop

Aug 17, 2009 at 10:45 PM

I have a repository that returns a list.


SandGridDataContext db = new SandGridDataContext();

        // Send List of Companies
        public List<Company> FindAllCompanies()
            return db.Companies.ToList();


I then try to serialize this list but I get the exception Self referencing loop. Is there any work around for this? Thanks.

CompanyRepository companyRepository = new CompanyRepository();
            List<Company> compaines = new List<Company>();

            compaines = companyRepository.FindAllCompanies();
            string json = JsonConvert.SerializeObject(compaines);

Aug 18, 2009 at 9:35 AM

Try looking in the help...

Nov 24, 2009 at 9:08 PM

Good day,

Try something like this:

StringWriter sw = new StringWriter();
JsonSerializer ser = new JsonSerializer();
ser.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
using (JsonWriter jw = new JsonTextWriter(sw))
    jw.Formatting = Formatting.Indented;
    ser.Serialize(jw, reference);

string exit = sw.ToString();
Thank you :)