linqjs group with a sum [英] linqjs group by with a sum

查看:235
本文介绍了linqjs group with a sum的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个简单的问题,但我正在努力使用linqjs语法。

This might seems like an easy question but I'm struggling with the linqjs syntax.

给出以下基本JSON:

Given the following basic JSON:

{
        "DateEvent": "2013-04-23 14:00:00Z",
        "DateRecord": "2013-04-23 14:00:00Z",
        "Amount": -9500,
        "Type": {
            "Description": "Capital"
        },
        "Currency": {
            "ID": "USD",
        }
}

使用linqjs如何返回每种货币的总额?

Using linqjs how can I return the total for each currency?

Enumerable.From(data)
    .GroupBy("{ currency: $.Currency.ID }", null, 
        function (key, g) {
            var result = {
                    currency: key.currency,
                    total: g.Sum($.Amount)
                }});

上面的代码不起作用。

推荐答案

你几乎拥有它。 GroupBy和Sum中的键选择器不正确。键选择器也需要是一个字符串。请尝试以下操作:

You almost had it. Your key selector in your GroupBy and Sum is incorrect. Also the key selector needs to be a string. Try the following:

var result = Enumerable.From(data).GroupBy("$.Currency.ID", null,
    function (key, g) {
        var result = {
            currency: key,
            total: g.Sum("$.Amount")
        }
        return result;
    }).ToArray();

这篇关于linqjs group with a sum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆