英文:
Asp.net core how to join models on razor page
问题
以下是翻译好的部分:
public partial class TblCo
{
public int IdCo { get; set; }
public string Compañia { get; set; } = null!;
public int IdLocalPuesto { get; set; }
public virtual TblLocalPuesto IdLocalPuestoNavigation { get; set; } = null!;
public virtual ICollection<TblEmp> TblEmps { get; set; } = new List<TblEmp>();
}
public partial class TblLocalPuesto
{
public int IdLocalPuestos { get; set; }
public int IdLocal { get; set; }
public int IdPuesto { get; set; }
public virtual TblLocal IdLocalNavigation { get; set; } = null!;
public virtual TblPuesto IdPuestoNavigation { get; set; } = null!;
public virtual ICollection<TblCo> TblCos { get; set; } = new List<TblCo>();
public virtual ICollection<TblInventario> TblInventarios { get; set; } = new List<TblInventario>();
public virtual ICollection<TblVenta> TblVentaIdLocalPuestoONavigations { get; set; } = new List<TblVenta>();
public virtual ICollection<TblVenta> TblVentaIdLocalPuestoVNavigations { get; set; } = new List<TblVenta>();
}
public partial class TblLocal
{
public int IdLocal { get; set; }
public string Nombre { get; set; } = null!;
public string? Pass { get; set; }
public bool? Activo { get; set; }
public int? Nivel { get; set; }
public virtual ICollection<TblLocalPuesto> TblLocalPuestos { get; set; } = new List<TblLocalPuesto>();
}
IQueryable<TblCo> compañia = (from c in _context.TblCos
join lp in _context.TblLocalPuestos on c.IdLocalPuesto equals lp.IdLocalPuestos
join l in _context.TblLocals on lp.IdLocal equals l.IdLocal
select new TblCo()
{
IdCo = c.IdCo,
Nombre = l.Nombre,
Compañia = c.Compañia
});
public class IndexModel : PageModel
{
private readonly usalentesWebApp.Models.UsalentesContext _context;
public IndexModel(usalentesWebApp.Models.UsalentesContext context)
{
_context = context;
}
public IList<TblCo> Compañia { get; set; } = default!;
public async Task OnGetAsync(string sortOrder, string searchString)
{
if (_context.TblCos != null)
{
IQueryable<TblCo> compañia =
from c in _context.TblCos
select new TblCo()
{
IdCo = c.IdCo,
Nombre = c.IdLocalPuestoNavigation.IdLocalNavigation.Nombre,
Compañia = c.Compañia
};
Compañia = await compañia.AsNoTracking().ToListAsync();
}
}
}
希望这对你有所帮助。
英文:
Good afternoon,
I have a project that i scaffolded from an existing database and it created tehe modeles and i don't understand how to join this models in one page.
models:
public partial class TblCo
{
public int IdCo { get; set; }
public string Compañia { get; set; } = null!;
public int IdLocalPuesto { get; set; }
public virtual TblLocalPuesto IdLocalPuestoNavigation { get; set; } = null!;
public virtual ICollection<TblEmp> TblEmps { get; set; } = new List<TblEmp>();
}
public partial class TblLocalPuesto
{
public int IdLocalPuestos { get; set; }
public int IdLocal { get; set; }
public int IdPuesto { get; set; }
public virtual TblLocal IdLocalNavigation { get; set; } = null!;
public virtual TblPuesto IdPuestoNavigation { get; set; } = null!;
public virtual ICollection<TblCo> TblCos { get; set; } = new List<TblCo>();
public virtual ICollection<TblInventario> TblInventarios { get; set; } = new List<TblInventario>();
public virtual ICollection<TblVenta> TblVentaIdLocalPuestoONavigations { get; set; } = new List<TblVenta>();
public virtual ICollection<TblVenta> TblVentaIdLocalPuestoVNavigations { get; set; } = new List<TblVenta>();
}
public partial class TblLocal
{
public int IdLocal { get; set; }
public string Nombre { get; set; } = null!;
public string? Pass { get; set; }
public bool? Activo { get; set; }
public int? Nivel { get; set; }
public virtual ICollection<TblLocalPuesto> TblLocalPuestos { get; set; } = new List<TblLocalPuesto>();
}
right now what i'm doing is this so that i can join the 3 tables on the razor page:
IQueryable<TblCo> compañia = (from c in _context.TblCos
join lp in _context.TblLocalPuestos on c.IdLocalPuesto equals lp.IdLocalPuestos
join l in _context.TblLocals on lp.IdLocal equals l.IdLocal
select new TblCo()
{
IdCo = c.IdCo,
Nombre = l.Nombre,
Compañia = c.Compañia
});
since the models have virtual links to the other models i think there must be a better way of doing it.
can anyone help?
Thanks!
EDIT:
this is my full page code.
the field NOMBRE comes from TblLocal and is not in TblCo,
how can i show it without adding the field to the model TblCo.
public class IndexModel : PageModel
{
private readonly usalentesWebApp.Models.UsalentesContext _context;
public IndexModel(usalentesWebApp.Models.UsalentesContext context)
{
_context = context;
}
public IList<TblCo> Compañia { get; set; } = default!;
public async Task OnGetAsync(string sortOrder, string searchString)
{
if (_context.TblCos != null)
{
IQueryable<TblCo> compañia =
from c in _context.TblCos
select new TblCo()
{
IdCo = c.IdCo,
Nombre = c.IdLocalPuestoNavigation.IdLocalNavigation.Nombre,
Compañia = c.Compañia
};
Compañia = await compañia.AsNoTracking().ToListAsync();
}
}
答案1
得分: 0
你已经拥有所有必要的导航属性,无需自己执行连接操作:
var compañia =
from c in _context.TblCos
select new TblCo()
{
IdCo = c.IdCo,
Nombre = c.IdLocalPuestoNavigation.IdLocalNavigation.Nombre,
Compañia = c.Compañia
};
英文:
You have all needed navigation properties to do not do joins by yourself:
var compañia =
from c in _context.TblCos
select new TblCo()
{
IdCo = c.IdCo,
Nombre = c.IdLocalPuestoNavigation.IdLocalNavigation.Nombre,
Compañia = c.Compañia
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论