Error when adding SUM

1

I'm trying to implement SUM within these lines of code to sum the two columns Litro and TotalGasto

Code:

var teste = consulta.Where(i => i.DtAbastecido >= dataInicio &&
                                i.DtAbastecido <= dataFinal)
                    .Sum(x =>x.Litro)
                    .GroupBy(x => new { x.NumCarro.NCarro })
                    .Select(x => x.First())
                    .OrderBy(x => x.NumCarro.NCarro);

But I'm getting the following error:

  

'int' does not contain a definition for 'GroupBy' and no extension method 'GroupBy' accepting a first argument of type 'int' could be found (are you missing a reference or an assembly reference?)

    
asked by anonymous 01.02.2018 / 20:35

1 answer

2

It's because the query does not make sense, first you're doing the sum and then trying to group.

Keep in mind, first, that Sum returns a integer . Therefore, it does not make sense to try to apply a GroupBy after using the Sum method.

You probably wanted to do this:

var teste = consulta.Where(i => i.DtAbastecido >= dataInicio &&
                                i.DtAbastecido <= dataFinal)
                    .GroupBy(x => new { x.NumCarro.NCarro })
                    .Select(x => x.First())
                    .Sum(x =>x.Litro);
    
02.02.2018 / 14:26