-1
Guys, I got two tables, sale and blindfolds: Sale:
[Table("venda")]
public class VendaModel
{
[Key]
[Required]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id_venda { get; set; }
public DateTime dt_venda { get; set; }
public int id_empresa { get; set; }
public int? id_cliente { get; set; }
public int? id_vendedor { get; set; }
public string vlr_total { get; set; }
public string perc_desc { get; set; }
public string vlr_desc { get; set; }
public string vlr_liq { get; set; }
public int forma_pgto { get; set; }
public int parcelas { get; set; }
public string tipo { get; set; } //orcamento ou venda
public int id_plano_conta { get; set; } //orcamento ou venda
[JsonIgnore]
[ForeignKey("id_empresa")]
public virtual EmpresaModel EmpresaModel { get; set; }
[JsonIgnore]
[ForeignKey("id_cliente")]
public virtual PessoaModel ClienteModel { get; set; }
[JsonIgnore]
[ForeignKey("id_vendedor")]
public virtual PessoaModel VendedorModel { get; set; }
public virtual ICollection<VendaItensModel> VendaItensModel { get; set; }
[JsonIgnore]
public virtual ICollection<ProdutoHistoricoModel> ProdutoHistoricoModel { get; set; }
}
Vendaitens:
[Table("venda_itens")]
public class VendaItensModel
{
[Key]
[Required]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id_venda_itens { get; set; }
public int id_venda { get; set; }
public int id_empresa { get; set; }
public int id_produto { get; set; }
public string qtde { get; set; }
public string preco_venda { get; set; }
public string total_item { get; set; }
[JsonIgnore]
[ForeignKey("id_empresa")]
public virtual EmpresaModel EmpresaModel { get; set; }
[JsonIgnore]
[ForeignKey("id_venda")]
public virtual VendaModel VendaModel { get; set; }
[ForeignKey("id_produto")]
public virtual ProdutoModel ProdutoModel { get; set; }
}
With the query below (I am using this way to search the desired fields, if you do an object search, comes fields q do not need), I look for sales, however I need to modify the code to fetch also the items of sale:
retorno = (from a in context.VendaModel
join b in context.Pessoa on a.id_cliente equals b.id_pessoa
join c in context.Pessoa on a.id_vendedor equals c.id_pessoa
where a.id_empresa == idEmpresa
&& a.dt_venda >= rDtInicio
&& a.dt_venda <= rDFim
&& a.tipo == tipo
select new
{
a.dt_venda,
a.forma_pgto,
a.id_cliente,
a.id_empresa,
a.id_venda,
a.id_vendedor,
a.parcelas,
a.perc_desc,
a.tipo,
a.vlr_desc,
a.vlr_liq,
a.vlr_total,
cliente = b.razao,
vendedor = c.razao
});
how could I bring the items of each sale, my view expects a Json with the sale and its items, more or less like this (in the example below I’m just exemplifying, I know q the json structure is not quite that way):
[
{
vendaX[]
itens venda X{}
vendaY
itens venda Y{}
}
]
The conversion to Json I know how to do, I need only modifies the Linq query, as would this query?
Product class
[Table("produto")]
public class ProdutoModel
{
[Key]
[Required]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id_produto { get; set; }
public int id_produto_grupo { get; set; }
public int id_fabricante { get; set; }
public int id_empresa { get; set; }
public string barras { get; set; }
public string nome { get; set; }
public string und { get; set; }
public string preco_compra { get; set; }
public string margem_lucro { get; set; }
public string preco_venda { get; set; }
public string perc_comissao { get; set; }
public string qtde_estoque { get; set; }
public string est_minimo { get; set; }
public string fracao { get; set; }
public int situacao { get; set; }
public string alldata { get; set; }
public string data_cad { get; set; }
public string perc_promocao { get; set; }
public string desc_maximo { get; set; }
[JsonIgnore]
[ForeignKey("id_empresa")]
public virtual EmpresaModel EmpresaModel { get; set; }
[JsonIgnore]
[ForeignKey("id_fabricante")]
public virtual PessoaModel PessoaModel { get; set; }
[JsonIgnore]
[ForeignKey("id_produto_grupo")]
public virtual ProdutoGrupoModel ProdutoGrupoModel { get; set; }
}
Pedro Paulo, I forgot to mention that I also need the name of the product, the rest worked 100%, man, thanks! just help me again with the product name.
– alessandre martins
I edited and put the product class
– alessandre martins
in vdd, that first block of code (var joinVendaItemVenda = context.VendaModel.Join(context.Vendaitensmodel,...) is bringing the sale, not the sale items. I edited again and pasted a return photo
– alessandre martins
I made the edits in my reply, if there is still something missing please let me know.
– Pedro Paulo
100% Pedro Paulo, thanks, I will study this code.
– alessandre martins
Any email or skype I can talk to you about ?
– alessandre martins
Pedro, there is still a little detail missing, I edited and pasted an image, I made a sale with two items, but the return shows the two items, I need to return a line, within this line the two items. That is, I need you to return the sale and within the sale your items.
– alessandre martins