Error when placing SUM

Asked

Viewed 89 times

1

I’m trying to implement the SUM within those 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 using Reference or an Assembly Reference?)

1 answer

2


It’s because the query doesn’t make sense, first you’re adding up and then trying to group.

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

You probably wanted to do it like 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);

Browser other questions tagged

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