Feat Selector Modo Tabla
This commit is contained in:
@@ -965,43 +965,37 @@ public class ResultadosController : ControllerBase
|
||||
|
||||
// 3. Procesar los datos por cada municipio
|
||||
var resultadosPorMunicipio = municipios.Select(municipio =>
|
||||
{
|
||||
// Filtramos los resultados solo para el municipio actual
|
||||
var resultadosDelMunicipio = resultadosCrudos.Where(r => r.AmbitoGeograficoId == municipio.Id);
|
||||
{
|
||||
var resultadosDelMunicipio = resultadosCrudos.Where(r => r.AmbitoGeograficoId == municipio.Id);
|
||||
|
||||
// Agrupamos por categoría (Senadores, Concejales, etc.)
|
||||
var resultadosPorCategoria = resultadosDelMunicipio
|
||||
.GroupBy(r => r.CategoriaId)
|
||||
.Select(g =>
|
||||
{
|
||||
var totalVotosCategoria = (decimal)g.Sum(r => r.CantidadVotos);
|
||||
|
||||
// Obtenemos los 2 partidos con más votos para esta categoría EN ESTE MUNICIPIO
|
||||
var ranking = g
|
||||
.OrderByDescending(r => r.CantidadVotos)
|
||||
.Take(2)
|
||||
.Select(r => new
|
||||
{
|
||||
NombreCorto = r.AgrupacionPolitica.NombreCorto ?? r.AgrupacionPolitica.Nombre,
|
||||
Porcentaje = totalVotosCategoria > 0 ? (r.CantidadVotos / totalVotosCategoria) * 100 : 0
|
||||
})
|
||||
.ToList();
|
||||
|
||||
return new
|
||||
{
|
||||
CategoriaId = g.Key,
|
||||
Ranking = ranking
|
||||
};
|
||||
})
|
||||
.ToDictionary(r => r.CategoriaId); // Lo convertimos a diccionario para fácil acceso
|
||||
|
||||
return new
|
||||
var resultadosPorCategoria = resultadosDelMunicipio
|
||||
.GroupBy(r => r.CategoriaId)
|
||||
.Select(g =>
|
||||
{
|
||||
MunicipioId = municipio.Id,
|
||||
MunicipioNombre = municipio.Nombre,
|
||||
ResultadosPorCategoria = resultadosPorCategoria
|
||||
};
|
||||
}).ToList();
|
||||
var totalVotosCategoria = (decimal)g.Sum(r => r.CantidadVotos);
|
||||
|
||||
var ranking = g
|
||||
.OrderByDescending(r => r.CantidadVotos)
|
||||
.Take(2)
|
||||
.Select(r => new
|
||||
{
|
||||
NombreCorto = r.AgrupacionPolitica.NombreCorto ?? r.AgrupacionPolitica.Nombre,
|
||||
Porcentaje = totalVotosCategoria > 0 ? (r.CantidadVotos / totalVotosCategoria) * 100 : 0,
|
||||
Votos = r.CantidadVotos // <-- AÑADIR ESTE CAMPO
|
||||
})
|
||||
.ToList();
|
||||
|
||||
return new { CategoriaId = g.Key, Ranking = ranking };
|
||||
})
|
||||
.ToDictionary(r => r.CategoriaId); // Lo convertimos a diccionario para fácil acceso
|
||||
|
||||
return new
|
||||
{
|
||||
MunicipioId = municipio.Id,
|
||||
MunicipioNombre = municipio.Nombre,
|
||||
ResultadosPorCategoria = resultadosPorCategoria
|
||||
};
|
||||
}).ToList();
|
||||
|
||||
// Devolvemos las categorías que tuvieron resultados en esta sección para construir la cabecera
|
||||
var categoriasMap = await _dbContext.CategoriasElectorales.AsNoTracking().ToDictionaryAsync(c => c.Id);
|
||||
|
||||
Reference in New Issue
Block a user