javascriptlinq.js

Linq.js Sum inside Where


Is there any way to filter in Linq.JS with SUM ?

My try:

var query = Enumerable
            .From(self.data())
            .Where("$$.Sum($.percent) > 100")
            .ToArray();

Error:

linq.js: Uncaught TypeError: $$.Sum is not a function


Solution

  • Here is the code that I used and solved:

    self.validatePercentualSumOfItens = () => {
        const sumOfGroupedItens = Enumerable
              .From(self.dadosCobranca().configuracoes())
              .Where("$.idAgrupadorRateio != null && $.idAgrupadorRateio != -1")
              .GroupBy("{ idAgrupadorRegra: $.idAgrupadorRegra, 
                          idAgrupadorRateio: $.idAgrupadorRateio 
                        }", 
                       "parseFloat($.percentualRateio)",
                       "{ idAgrupadorRegra: $.idAgrupadorRegra, 
                          idAgrupadorRateio: $.idAgrupadorRateio, 
                          total: parseFloat($$.Sum()).toFixed(2) 
                        }",
                        "$.idAgrupadorRegra + '-' + $.idAgrupadorRateio")
               .ToArray();
    
        const itensThatNotMatchAHundred = Enumerable
                                    .From(sumOfGroupedItens)
                                    .Where("$.total < 100 || $.total > 100")
                                    .ToArray();
    
        return itensThatNotMatchAHundred.length;
    };