0

I need to build a very complex view and query maybe. I have 3 tables in my database: Lote, Carga and RegistosForno. My model has all these 3 classes:

Class "Carga"

public partial class carga
{
    public string cga_numero { get; set; }
    public string cem_numero { get; set; }
    public string lot_numero { get; set; }
    public string tpe_numero { get; set; }
    public string buj_numero { get; set; }
    public string ope_numero { get; set; }
    public Nullable<decimal> for_numero { get; set; }
    public string ret_numero { get; set; }
    public Nullable<decimal> cga_carga { get; set; }
    public Nullable<int> cga_parte_lote { get; set; }
    public Nullable<decimal> cga_data_in_forno { get; set; }
    public Nullable<decimal> cga_hora_in_forno { get; set; }
    public string cga_tmp_in_forno { get; set; }
    public Nullable<decimal> cga_data_out_forno { get; set; }
    public Nullable<decimal> cga_hora_out_forno { get; set; }
    public string cga_tmp_out_forno { get; set; }
    public Nullable<int> cga_estado { get; set; }
    public Nullable<int> cga_cem_num_utilizacoes { get; set; }
    public Nullable<int> cga_ret_total_utilizacoes { get; set; }
    public Nullable<int> cga_ret_num_utilizacoes { get; set; }
    public Nullable<int> cga_ret_lim_utilizacoes { get; set; }
    public Nullable<int> cga_buj_total_utilizacoes { get; set; }
    public Nullable<int> cga_buj_num_utilizacoes { get; set; }
    public Nullable<int> cga_buj_lim_utilizacoes { get; set; }
    public string cga_lot_tmp_out_forno { get; set; }
    public string cga_lot_tmp_in_forno { get; set; }
    public Nullable<int> cga_lot_cem_num_utilizacoes { get; set; }
    public string cga_datahora { get; set; }
}

Class "Lote"

public partial class lote
{
    public string lot_numero { get; set; }
    public string ope_numero { get; set; }
    public string tpe_nome { get; set; }
    public Nullable<int> lot_peso_total { get; set; }
    public Nullable<int> lot_total_cargas { get; set; }
    public Nullable<int> lot_cargas { get; set; }
    public Nullable<int> lot_estado { get; set; }
    public Nullable<decimal> lot_data { get; set; }
    public Nullable<decimal> lot_hora { get; set; }
    public Nullable<decimal> lot_peso_prp { get; set; }
    public Nullable<decimal> lot_peso_total_cargas { get; set; }
    public Nullable<int> lot_cem_num_utilizacoes { get; set; }
    public Nullable<int> lot_cem_tipo { get; set; }
    public string lot_datahora { get; set; }

}

Class "RegForno"

public partial class regforno
{
    public int reg_id { get; set; }
    public string cga_numero { get; set; }
    public Nullable<decimal> for_numero { get; set; }
    public string ope_numero { get; set; }
    public Nullable<int> reg_id_plc { get; set; }
    public Nullable<decimal> reg_hora { get; set; }
    public Nullable<decimal> reg_data { get; set; }
    public Nullable<int> reg_temperatura { get; set; }
    public Nullable<int> reg_estado { get; set; }
    public Nullable<int> reg_cga_tmp_forno { get; set; }
    public string reg_datahora { get; set; }
}

My view should look like this:

Lote 1

>Carga 1
    >Registo 1 forno Carga 1
    >Registo 2 forno Carga 1
>Carga 2
    >Registo 1 forno Carga 2
    >Registo 2 forno Carga 2
>Carga 3
    >Registo 1 forno Carga 3
    >Registo 2 forno Carga 3
>Carga 4
    >Registo 1 forno Carga 4
    >Registo 2 forno Carga 4

Lote N

>Carga 1
    >Registo 1 forno Carga 1
    >Registo 2 forno Carga 1
>Carga 2
    >Registo 1 forno Carga 2
    >Registo 2 forno Carga 2
>Carga 3
    >Registo 1 forno Carga 3
    >Registo 2 forno Carga 3
>Carga 4
    >Registo 1 forno Carga 4
    >Registo 2 forno Carga 4

Basically I need to: For each Lote get all the records from Carga related to that lote and get all the records from regForno related to that Carga.

My experience with asp.net is not much but I have been able to implement a few things mostly based on tutorials and examples. However I haven't been able to find an example for this or any tutorial doing this kind of thing. I believe is very specific from my user requests.

Mark
  • 2,380
  • 11
  • 29
  • 49
aganju82
  • 43
  • 4

2 Answers2

0

Are you using Entity Framework as your ORM? My practice is using LINQ to join your domain classes in the data access layer and then project the results into a viewmodel. If you're using EF then you can leverage the fluent API to set your relationships. This viewmodel should be passed into your view. I would strongly advise against passing these domain classes directly into your views given your requirements, but again I don't know how or if you've abstracted your data in the project.

Please refer to ViewModel Best Practices for more details.

Community
  • 1
  • 1
Todd
  • 459
  • 5
  • 18
0

There should be a relationship between your classes like,

public partial class carga
{
public int Id{get; set;}
..
..
public List<lote> lots{get;set;} 
}

public partial class lote
{
public int Id{get; set;}
..
..
public List<reg> regs{get;set;} 
}

public partial class reg
{
public int Id{get; set;}
..
..

}

If you prepare your classes like so using Entity framework or something etc. In your controller,

 List<cargs> list=context.Carga.ToList();
    return View();

In View,

@foreach(var item in Model)
{
<h2>@item.Name</h2>
List<Lots> listm=Model.Lots.Where(m=>m.Id==item.Id).ToList();
foreach(var i in listm)
{
<h3>@i.Name</h3>
List<Regs> listr=i.Regs.Where(m=>m.Id==i.Id).ToList();
foreach(var j in listr)
{
<h4>@j.Name</h4>


   }

}
}

hope it helps.

Karthik Bammidi
  • 1,851
  • 9
  • 31
  • 65
  • Hi and thanks for your answer. Taking advantage of your ideas and with some more hints from more experienced people I eventually got it. I have created the relationships between the classes and I ha created the View model in the controller which became more complex that your line. It is working now, thanks for your help. – aganju82 Sep 19 '13 at 15:07