add lambda expression column

2

I need to return the sum of the business_value column, 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 also bring the sum of the business_value column? example: if the database has 10 records, and each record the business_value is 10, it needs to be returned 100

    
asked by anonymous 02.11.2017 / 13:48

1 answer

1

If what you need is just the sum of valor_negocio , then you can discard the select operator 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 , you will have the total added in the local variable somaValorNegocio .

I imagine this is what I was looking for.

    
02.11.2017 / 18:36