如何在Razor页面上使用Asp.net Core连接模型

huangapple go评论60阅读模式
英文:

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&ntilde;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&ntilde;ia = c.Compa&ntilde;ia
                                });
public class IndexModel : PageModel
{
    private readonly usalentesWebApp.Models.UsalentesContext _context;

    public IndexModel(usalentesWebApp.Models.UsalentesContext context)
    {
        _context = context;
    }

    public IList<TblCo> Compa&ntilde;ia { get; set; } = default!;

    public async Task OnGetAsync(string sortOrder, string searchString)
    {
        if (_context.TblCos != null)
        {
            IQueryable<TblCo> compa&ntilde;ia =
                                        from c in _context.TblCos
                                        select new TblCo()
                                        {
                                            IdCo = c.IdCo,
                                            Nombre = c.IdLocalPuestoNavigation.IdLocalNavigation.Nombre,
                                            Compa&ntilde;ia = c.Compa&ntilde;ia
                                        };

            Compa&ntilde;ia = await compa&ntilde;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&#241;ia { get; set; } = null!;

    public int IdLocalPuesto { get; set; }

    public virtual TblLocalPuesto IdLocalPuestoNavigation { get; set; } = null!;

    public virtual ICollection&lt;TblEmp&gt; TblEmps { get; set; } = new List&lt;TblEmp&gt;();
}
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&lt;TblCo&gt; TblCos { get; set; } = new List&lt;TblCo&gt;();

    public virtual ICollection&lt;TblInventario&gt; TblInventarios { get; set; } = new List&lt;TblInventario&gt;();

    public virtual ICollection&lt;TblVenta&gt; TblVentaIdLocalPuestoONavigations { get; set; } = new List&lt;TblVenta&gt;();

    public virtual ICollection&lt;TblVenta&gt; TblVentaIdLocalPuestoVNavigations { get; set; } = new List&lt;TblVenta&gt;();
}
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&lt;TblLocalPuesto&gt; TblLocalPuestos { get; set; } = new List&lt;TblLocalPuesto&gt;();
}

right now what i'm doing is this so that i can join the 3 tables on the razor page:

IQueryable&lt;TblCo&gt; compa&#241;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&#241;ia = c.Compa&#241;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&lt;TblCo&gt; Compa&#241;ia { get; set; } = default!;

    public async Task OnGetAsync(string sortOrder, string searchString)
    {
        if (_context.TblCos != null)
        {
            IQueryable&lt;TblCo&gt; compa&#241;ia =
                                        from c in _context.TblCos
                                        select new TblCo()
                                        {
                                            IdCo = c.IdCo,
                                            Nombre = c.IdLocalPuestoNavigation.IdLocalNavigation.Nombre,
                                            Compa&#241;ia = c.Compa&#241;ia
                                        };

            Compa&#241;ia = await compa&#241;ia.AsNoTracking().ToListAsync();

        }
    }

答案1

得分: 0

你已经拥有所有必要的导航属性,无需自己执行连接操作:

var compa&#241;ia = 
    from c in _context.TblCos
    select new TblCo()
    {
        IdCo = c.IdCo,
        Nombre = c.IdLocalPuestoNavigation.IdLocalNavigation.Nombre,
        Compa&#241;ia = c.Compa&#241;ia
    };
英文:

You have all needed navigation properties to do not do joins by yourself:

var compa&#241;ia = 
    from c in _context.TblCos
    select new TblCo()
    {
        IdCo = c.IdCo,
        Nombre = c.IdLocalPuestoNavigation.IdLocalNavigation.Nombre,
        Compa&#241;ia = c.Compa&#241;ia
    };

huangapple
  • 本文由 发表于 2023年7月7日 04:40:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定