Return problem in Enumerable in WebApi

1

I'm doing a webApi, which has its entities and its context class that it creates through the database, with its respective command.

Everything is working perfectly for me, the drama is when I want to return a set of objects in JSON and it only returns one.

I have a context class, which makes dependency injections for all entities.

If in my [HttpGet] I do this:

public IEnumerable<Sucursales> Get()
    {
        using (var _context = new hn_asis_testContext())
        {
           return _context.Sucursales.ToList();
        }
    }

Return this to me:

[
  {
    "sucursalId": 2,
    "nombre": "Santa Clara",
    "barrioId": 1,
    "empleados": [

    ],
    "ventas": [

    ],
    "barrio": null
  },
  {
    "sucursalId": 3,
    "nombre": "Santa Clara",
    "barrioId": 1,
    "empleados": [

    ],
    "ventas": [

    ],
    "barrio": null
  },
  {
    "sucursalId": 4,
    "nombre": "Santa Clara",
    "barrioId": 1,
    "empleados": [

    ],
    "ventas": [

    ],
    "barrio": null
  },
  {
    "sucursalId": 5,
    "nombre": "Santa Clara",
    "barrioId": 2,
    "empleados": [

    ],
    "ventas": [

    ],
    "barrio": null
  }
]

What I want to achieve, is that in each Branch, with the neighborhood ID that I give him his neighborhood object, where Barrio = null does not say.

Then I implemented this:

public IEnumerable<Sucursales> Get()
    {
        using (var _context = new hn_asis_testContext())
        {

              var lista = new List<Sucursales>();
              var lista2 = new List<Barrios>();
              lista = _context.Sucursales.ToList();
              lista2 = _context.Barrios.ToList();
              foreach (var item in lista)
              {  
                     item.Barrio = lista2.FirstOrDefault(t => t.BarrioId == item.BarrioId);

              }
              return lista.ToList();

        }

    }

Which, does not return me Json set, but ONE only with the goal I wanted to reach.

Asi:

[
  {
    "sucursalId": 2,
    "nombre": "Santa Clara",
    "barrioId": 1,
    "empleados": [

    ],
    "ventas": [

    ],
    "barrio": {
      "barrioId": 1,
      "nombre": "Santa Clara",
      "clientes": [

      ],
      "empleados": [

      ],
      "sucursales": [

      ]
    }
  }
]
    
asked by Alexis Zapata 14.07.2017 в 02:49
source

2 answers

1

The main problem with your query is due to the processing you are doing. Imagine that you have 100,000 branches, then you would have to do 100,000 subqueries to obtain the neighborhoods associated with each branch.

The solution lies in making a JOIN between tables Sucursales and Barrios :

public IEnumerable<Sucursales> Get()
{
    using (var _context = new hn_asis_testContext())
    {
        var lista = (from p in _context.Sucursales
                     join o in _context.Barrios on p.BarrioId equals o.BarrioId
                     select new {
                            sucursalId = p.sucursalId,
                            nombre = p.nombre,
                            barrioId = p.barrioId,
                            empleados = p.empleados,
                            ventas = p.ventas,
                            barrio = o.nombre
                     }).ToList();

        return lista;
}
    
answered by 14.07.2017 в 20:43
0

Take a look at this answer in stackoverflow: link , maybe it will help.

Code that solves the problem:

    services.AddMvc().AddJsonOptions(options => { 
      options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
 });
    
answered by 15.07.2017 в 22:45