How to use group by in LAMBDA

Asked

Viewed 7,455 times

0

I have a product table:

int id
string descricao
int quant

Here’s what I wanna do:

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

How to make the above query in lambda?

1 answer

5


There are two ways to do it:

Straight into the Linum

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) });
  • only managed to catch the descricao and the quant, if I have more fields, how do I catch them?

  • 1

    Just add the other fields separated by comma

  • I could not :/ I will create another question showing how I am doing to see if you help me.

Browser other questions tagged

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