Get Total Relationship Items 1:N

Asked

Viewed 107 times

2

I have two Product and Items tables - these tables have Father/Son relationship respectively. I would like to know how to get the total of items in the Items table knowing that each customer can own more than one Order and that each order consists of several items? If possible, I would also like to know how I do to get the total value of the items.

I’m using Entityframework with Code First. The query already has Include but so far when adding Groupby + Count the returned result is the total of Requests (parent table).

Thanks in advance for the strength.

  • Hello @urlflavio. Welcome to Stackoverflow in English. Take a [tour] to understand how the site works. Taking advantage, could post what you have code? Only with this information is complicated help you. Post the model of your classes and try to explain a little more what you want.

  • @urlflavio, could you post some code? What do you want? It’s not clear, you want the query, or code in c# that will execute the query for you?

1 answer

3


I’m guessing the following construction of your Models:

public class Produto
{
    [Key]
    public int ProdutoId { get; set; }

    [Required]
    public int Nome { get; set; }
    [Required]
    public decimal Preco { get; set; }

    public virtual ICollection<PedidoProduto> PedidoProdutos { get; set; } // Aqui seria sua relação de itens
}

public class Pedido
{
    [Key]
    public int PedidoId { get; set; }
    public int ClienteId { get; set; }

    public virtual Cliente Cliente { get; set; }

    public virtual ICollection<PedidoProduto> PedidoProdutos { get; set; }
}

public class PedidoProduto
{
    [Key]
    public int PedidoProdutoId { get; set; }
    public int PedidoId { get; set; }
    public int ProdutoId { get; set; }

    [Required]
    public int Quantidade { get; set; }

    public virtual Pedido Pedido { get; set; }
    public virtual Produto Produto { get; set; }
}

I would like to know how to get the total of items in the Items table knowing that each customer can own more than one Order and that each order consists of several items?

It can be done like this:

var pedido = db.Pedidos.FirstOrDefault(p => p.PedidoId == 1);
var itens = pedido.PedidoProdutos.Select(pp => pp.Produto).ToList();

If possible, I would also like to know how I do to get the total value of the items.

Thus:

var total = pedido.PedidoProdutos.Sum(pp => pp.Produto.Preco * pp.Quantidade);
  • People, first of all I would like to thank you for the help of all! When reading my question I saw that I really did not make very clear my doubt.

  • Guys, first of all I’d like to thank you for all your help! Gypsy Morrison, seeing his example the same has a logic more suited to my need. My only doubt is, should I save in the three tables each new request? if yes, how would I save in the three via Entity Framework? Thanks in advance for the strength!

  • The correct thing is you save in Pedido and PedidoProduto. For this new question, the best is a new question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.