How to use group by in LAMBDA

-1

I have a Products table:

int id
string descricao
int quant

I want to do the following:

select descricao, sum(quant) from produtos group by descricao

How to do the above query in lambda ?

    
asked by anonymous 24.04.2017 / 01:00

1 answer

3

There are two ways to do it:

Direct on linq

var result=from p in produto group p by p.descricao into g select new {descricao=g.Key,count=g.Sum(x=>x.quant)}

or with Extended methods

var result = produto.GroupBy(x => x.descricao).Select(new { descricao = g.Key, count = g.Sum(x => x.quant) });
    
24.04.2017 / 01:16