Add subitems between groups with Linnus

Asked

Viewed 209 times

1

How can I perform calculations between subsets of a group by query in English? Follow example:

Consulta Linq

var listPlanoDeContas = (from t in _planoDeContasRepository.GetAll()
                         orderby t.Month ascending
                         group t by t.DfpChartOfAccounts into dfpGroup
                         select dfpGroup).ToList();

To illustrate, this query returns something like:

Plano de Contas      |    Mês 1    |    Mês 2     |    Mês 3    | ...

Receitas Totais           10000,00        13000,00      8000,00
Deduções                    878,00         1020,00       780,00
Custos                     1345,00         1478,00       850,00
Despesas                   3789,00         4342,00      1678,00
______________________________________________________________________
Saldo de Caixa                ?               ?             ?

I need to find the cash balance. Then I would like a help to calculate via Linq or Lambda the values between each item of the account plan per month. Ie:

Total Month Revenue 1 - Month Deductions 1 - Month Costs 1 - Month Expenses 1

Thanks in advance.

  • Do you already get the table exactly as it is in your example? If so wouldn’t it just be creating a class to store the total of each month? I’ll create an answer based on that and you see if it meets.

2 answers

1

Note that in the example below I am using an example class and hypothetical fields of your class.

But I believe that only the code below meets your need, if your return on listPlanoDeContas for what you posted in the question.

SaldoCaixa =
new TotaisSaldoCaixa
{
    Total_Janeiro = listPlanoDeContas
        .Select(c => c.ReceitaJan - c.DeducoesJan - c.CustosJan - c.DespesasJan).SingleOrDefault(),

    Total_Fevereiro = listPlanoDeContas
        .Select(c => c.ReceitaFeb - c.DeducoesFeb - c.CustosFeb - c.DespesasFeb).SingleOrDefault(),

    Total_Marco = listPlanoDeContas
        .Select(c => c.ReceitaMar - c.DeducoesMar - c.CustosMar - c.DespesasMar).SingleOrDefault(),
    //Restante dos meses...
};
  • Thanks for the example and attention, but in fact I will I made a query with a Groupby per month adding up the values. Then I made a sub-query of the previous query with Where of the category "Recipes" and made a subtraction. The answer I will post here

1


I decided as follows:

//Fiz uma consulta geral no DB e agrupei por mês
var qry = _planoDeContasRepository.GetAll()
	 .GroupBy(x => x.Month).ToList();

//Pecorre a consulta aplicando o filtro e realizando o cálculo
foreach(var i in qry)
{
    var valorReceita = i.Where(x => x.Categoria.Equals("Receita")).Select(x => x.Valor).ToList();
    decimal valor = Convert.ToDecimal(valorReceita[0]);
    
    var somaTotalPorMes = i.Sum(x => x.Valor);

    decimal saldo = Convert.ToDecimal(somaTotalPorMes) - valor;
}

//Retorno o saldo e....

Browser other questions tagged

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