sum column lambda expression

Asked

Viewed 358 times

2

I need to return the sum of the column value_business, I have the following expression

dynamic contato = (from a in context.Negocio
                   join cli in context.Pessoa on a.id_cliente equals cli.id_pessoa
                   join col in context.Pessoa on a.id_colaborador equals col.id_pessoa
                   where cli.id_empresa == idEmpresa                                               
                   && a.data_inicio >= dataInicial
                   && a.data_inicio <= dataFinal
                   && a.status == "Contato"
                   && (idCliente == 0 ? a.id_cliente != 0 : a.id_cliente == idCliente)
                   && (idColaborador == 0 ? a.id_colaborador != 0 : a.id_colaborador == idColaborador)
                   select new
                   {
                       a.id_negocio,
                       a.id_colaborador,
                       a.id_cliente,
                       a.id_empresa,
                       cli = cli.razao,
                       col = col.razao,
                       a.titulo,
                       a.descricao,
                       a.valor_mensalidade,
                       a.valor_negocio,
                       a.cli_primens,
                       //a.data_inicio,
                       //a.data_fim,
                       a.status                                                   
                   }).ToList();

how to change to bring also the sum of the column value_business? example: if the database has 10 records, and each record the business value is 10, need to be returned 100

  • Whether to group values ????

  • Virgilio, I need to add the values, if the bank has 10 records, and each record the business value is 10, need to be returned 100

  • Need to group so this will return depending on the grouping 1 row for each ! that’s what you need?

  • I want only one line with the total (from the value field ) of all records

1 answer

1

If what you need is just the sum of valor_negocio, then you can discard the operator select, because it is only used if you want to return data from each record:

var somaValorNegocio =
               (from a in context.Negocio
               join cli in context.Pessoa on a.id_cliente equals cli.id_pessoa
               join col in context.Pessoa on a.id_colaborador equals col.id_pessoa
               where cli.id_empresa == idEmpresa                                               
               && a.data_inicio >= dataInicial
               && a.data_inicio <= dataFinal
               && a.status == "Contato"
               && (idCliente == 0 ? a.id_cliente != 0 : a.id_cliente == idCliente)
               && (idColaborador == 0 ? a.id_colaborador != 0 : a.id_colaborador == idColaborador)
               select a.valor_negocio)
               .Sum();

Now depending on the type of valor_negocio, Voce will have the total sum in the local variable somaValorNegocio.

I imagine this is what you were looking for.

  • Asked to put a select or a group

  • @alessandremartins, really when using this Linq syntax, it is necessary to use select or group. To not rewrite every query, just put select and remove the argument from Sum. I fixed it in the code, see if fucnion.

Browser other questions tagged

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