pick up customer with certain types of purchases

Asked

Viewed 66 times

0

I have these tables:

Sales : Idvenda(int)PK, Datavenda(Datetime), Qdeitem(Decimal), Valuation(Decimal). Iditem(FK),Idcliente(FK)

Item: Iditem(int)PK,Descricao(Varchar),Valoritem(Decimal)

Client:Idclient(int)PK,Name(varchar),Datacadastro(Datetime)

How do I filter only customers who bought 5 or more bikes and at least one tire using LINQ only?

  • In the application, customer has a list of Sales?

  • You use Entityframework?

  • This should be done with normal LINQ or Sql. Only need among a list of things sold, the Linq or sql should return who bought more than five cars and a tire.

1 answer

1

you can carry out the following consultation:

public IEnumerable<int> getClientes(int itemID, int qtdMinimo)
{
    return (
        from venda in db.Vendas
        where venda.IDItem == itemID
        group venda by venda.IDCliente into grupo
        where grupo.Sum(venda => venda.QdeItem) >= qtdMinimo
        select grp.Key
    );
}

In the above case, all customers who purchased more than 5 items of the informed product will be returned, regardless of whether these products were purchased in the same order.

P.S.: Has not been tested

Browser other questions tagged

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