2

我尝试了使用微风和 API 控制器加载项目列表的不同方法,使用过滤器(部分使用自定义对象,另一部分使用 ODataQueryOptions),但没有一个真正成功。

javascript中的测试代码:

    function test() {
        EntityQuery
            .from("Products")
            /*
            .withParameters({
                filters: [
                { column: "Name", value: "12" }
            ]})
            */
            .orderBy("Name desc")
            .skip(30)
            .take(15)
            .inlineCount(true)
            .using(manager)
            .execute()
            .then(success)
            .fail(fail);

        function success(data) {
            console.log(data.products);
            console.log(data.inlineCount);
        }
        function fail(error) {
            console.log(error.message);
        }
    };
    test();

理想情况下,我想用这样的东西来完成这个:

public IQueryable<Product> Products([FromUri] FilterObject[] filters, ODataQueryOptions odataQueryOptions)
        {
            var result = DummyData.GetProducts();            
            //var totalRowCount = result.Count();

            return result;
        }

数据将在其他地方过滤(使用 nHibernate),我删除了用于解析过滤器的部分等。但是,这永远不会起作用,因为另一层将返回总行数。

所以我尝试将其替换为:

public QueryResult Products([FromUri] FilterObject[] filters, ODataQueryOptions odataQueryOptions)
{
...
        return new QueryResult
            {
                InlineCount = totalRowCount,
                Results = result
            };
}

这会引发错误:无法创建 EDM 模型,因为控制器“产品”上的操作“产品”具有未实现 IEnumerable 的返回类型“Breeze.WebApi.QueryResult”。

删除 ODataQueryOptions 变量时错误消失。搜索没有给我有价值的反馈。

我试过这个:

public PageResult<Product> Products([FromUri] FilterObject[] filters, ODataQueryOptions odataQueryOptions)
{
....
    return new PageResult<Product>(result, null, totalRowCount);  
}

这不会引发错误。当打开返回的数据对象包含值未定义的 inlineCount 参数时,实际数据位于嵌套结果数组(Count、Items 和 NextPageLink)的第一项中。

这是让它工作的唯一方法吗?

通过将 ODataQueryOptions 作为参数添加到 TodoLists 方法,可以在 Bread 的 NoDb 示例中重现这一点:

    // GET ~/breeze/BreezeTodo/TodoList
    [HttpGet]
    public IQueryable<TodoList> TodoLists(ODataQueryOptions odataQueryOptions)
    //public IQueryable<TodoList> TodoLists()
    {
        var result = _repository.TodoLists;
        result = result.OrderByDescending(t => t.TodoListId);

        return result;
    }

使用

        return breeze.EntityQuery
            .from("TodoLists")
            .inlineCount(true)
            .skip(0).take(15)
            .using(manager).execute()
            .then(getSucceeded)
            .fail(getFailed);

请求如下所示:

GET /breeze/Todo/TodoLists?$top=15&$inlinecount=allpages HTTP/1.1

ODataQueryOptions 的小提琴结果:

[{"$id":"1","$type":"NoDb.Models.TodoList, NoDb","TodoListId":1,"Title":"Before work","Todos":[{"$id":"2","$type":"NoDb.Models.TodoItem, NoDb","TodoItemId":1,"Title":"Make coffee","IsDone":false,"TodoListId":1,"TodoList":{"$ref":"1"}},{"$id":"3","$type":"NoDb.Models.TodoItem, NoDb","TodoItemId":2,"Title":"Turn heater off","IsDone":false,"TodoListId":1,"TodoList":{"$ref":"1"}}]}]

并且没有:

{"$id":"1","$type":"Breeze.WebApi.QueryResult, Breeze.WebApi","Results":[{"$id":"2","$type":"NoDb.Models.TodoList, NoDb","TodoListId":1,"Title":"Before work","Todos":[{"$id":"3","$type":"NoDb.Models.TodoItem, NoDb","TodoItemId":1,"Title":"Make coffee","IsDone":false,"TodoListId":1,"TodoList":{"$ref":"2"}},{"$id":"4","$type":"NoDb.Models.TodoItem, NoDb","TodoItemId":2,"Title":"Turn heater off","IsDone":false,"TodoListId":1,"TodoList":{"$ref":"2"}}]}],"InlineCount":1}
4

1 回答 1

3

解决方案似乎是:

public QueryResult TodoLists(ODataQueryOptions<TodoList> odataQueryOptions)

提琴手:

要求:

GET /breeze/Todo/TodoLists?$top=15&$inlinecount=allpages HTTP/1.1

回复:

{"$id":"1","$type":"Breeze.WebApi.QueryResult, Breeze.WebApi","Results":[{"$id":"2","$type":"NoDb.Models.TodoList, NoDb","TodoListId":1,"Title":"Before work","Todos":[{"$id":"3","$type":"NoDb.Models.TodoItem, NoDb","TodoItemId":1,"Title":"Make coffee","IsDone":false,"TodoListId":1,"TodoList":{"$ref":"2"}},{"$id":"4","$type":"NoDb.Models.TodoItem, NoDb","TodoItemId":2,"Title":"Turn heater off","IsDone":false,"TodoListId":1,"TodoList":{"$ref":"2"}}]}],"InlineCount":1}
于 2013-09-19T07:57:04.527 回答