orderBy linq.js中的分组集合

时间:2014-09-21 21:19:47

标签: linq.js

这是不起作用的代码:

var grouping = Enumerable.from(periods)
                    .select(function(p){return new PeriodViewModel(p)})
                    .groupBy("$.periodNumber")
                    .orderBy("$.date")
                    .toArray();

periodviewmodels按periodNumber分组,日期应按日期递增排序,但这不会发生,我想是因为orderBy中的选择器应该以某种方式引用分组结果......

我不知道。这是测试数据,所以你可以自己尝试:

{
    "periods": [
        {
            "id": 1,
            "content": "this is not a test",
            "date": 1,
            "periodNumber": 1,
            "documents": [
                {
                    "name": "Homework.doc"
                },
                {
                    "name": "Todo.xls"
                }
            ]
        },
        {
            "id": 3,
            "content": "this might be a test",
            "date": 2,
            "periodNumber": 1,
            "documents": [
                {
                    "name": "Homework.doc"
                },
                {
                    "name": "Todo.xls"
                }
            ]
        },
        {
            "id": 5,
            "content": "This is a clause",
            "date": 3,
            "periodNumber": 1,
            "documents": [
                {
                    "name": "Homework.doc"
                },
                {
                    "name": "Todo.xls"
                }
            ]
        },
        {
            "id": 1,
            "content": "and not sth to share for all",
            "date": 5,
            "periodNumber": 2,
            "documents": [
                {
                    "name": "Homework.doc"
                },
                {
                    "name": "Todo.xls"
                }
            ]
        },
        {
            "id": 2,
            "content": "hello kitty???",
            "date": 2,
            "periodNumber": 3,
            "documents": [
                {
                    "name": "Homework.doc"
                },
                {
                    "name": "Todo.xls"
                }
            ]
        },
        {
            "id": 1,
            "content": "Flynnie winnie",
            "date": 1,
            "periodNumber": 3,
            "documents": [
                {
                    "name": "Homework.doc"
                },
                {
                    "name": "Todo.xls"
                }
            ]
        },
        {
            "id": 1,
            "content": "what do yo say about chol and ra?",
            "date": 1,
            "periodNumber": 4,
            "documents": [
                {
                    "name": "Homework.doc"
                },
                {
                    "name": "Todo.xls"
                }
            ]
        }
    ]
}

1 个答案:

答案 0 :(得分:1)

在对项目进行分组之前对其进行排序。无法从群组中访问date

var query = Enumerable.from(periods)
    .select(function (p){ return new PeriodViewModel(p); })
    .orderBy("$.date")
    .groupBy("$.periodNumber")
    .toArray();