83 lines
2.5 KiB
C#
83 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using bdAsegasa.dbcontext;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace bdAsegasa.db
|
|
{
|
|
public partial class municipios
|
|
{
|
|
private static List<municipios> _listaPoblaciones;
|
|
|
|
public string PoblacionYProvincia
|
|
{
|
|
get
|
|
{
|
|
try
|
|
{
|
|
if (this.Nombre == this.CodigoProvinciaNavigation?.Nombre)
|
|
{
|
|
return this.Nombre;
|
|
}
|
|
else
|
|
{
|
|
return $"{this.Nombre} ({this.CodigoProvinciaNavigation?.Nombre})";
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
return "";
|
|
}
|
|
}
|
|
}
|
|
|
|
public static string ObtienePoblacion(string codigoPoblacion)
|
|
{
|
|
EnsureCache();
|
|
var pob = _listaPoblaciones.FirstOrDefault(x => x.CodigoMunicipio == codigoPoblacion);
|
|
if (pob != null) return pob.Nombre;
|
|
|
|
using (var bd = tscgestionasegasa.NuevoContexto())
|
|
{
|
|
pob = bd.municipios.Include(x => x.CodigoProvinciaNavigation).FirstOrDefault(x => x.CodigoMunicipio == codigoPoblacion);
|
|
if (pob != null)
|
|
{
|
|
_listaPoblaciones.Add(pob);
|
|
return pob.Nombre;
|
|
}
|
|
}
|
|
return "";
|
|
}
|
|
|
|
public static string ObtieneProvincia(string codigoPoblacion)
|
|
{
|
|
EnsureCache();
|
|
var pob = _listaPoblaciones.FirstOrDefault(x => x.CodigoMunicipio == codigoPoblacion);
|
|
if (pob != null) return pob.CodigoProvinciaNavigation?.Nombre ?? "";
|
|
|
|
using (var bd = tscgestionasegasa.NuevoContexto())
|
|
{
|
|
pob = bd.municipios.Include(x => x.CodigoProvinciaNavigation).FirstOrDefault(x => x.CodigoMunicipio == codigoPoblacion);
|
|
if (pob != null)
|
|
{
|
|
_listaPoblaciones.Add(pob);
|
|
return pob.CodigoProvinciaNavigation?.Nombre ?? "";
|
|
}
|
|
}
|
|
return "";
|
|
}
|
|
|
|
private static void EnsureCache()
|
|
{
|
|
if (_listaPoblaciones == null)
|
|
{
|
|
using (var bd = tscgestionasegasa.NuevoContexto())
|
|
{
|
|
_listaPoblaciones = bd.municipios.Include(x => x.CodigoProvinciaNavigation).ToList();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|