Breeze Web API:如何将Query Result与ODataQueryOptions结合使用以返回inlineCount

时间:2013-09-18 16:55:06

标签: odata breeze asp.net-web-api

我尝试了使用breeze和API Controller加载项目列表的不同方法,使用过滤器(部分使用自定义对象和使用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模型,因为控制器'Product'上的操作'Products'具有不实现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方法,可以在NoDb breeze示例中重现:

    // 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}

1 个答案:

答案 0 :(得分:3)

解决方案似乎是:

public QueryResult TodoLists(ODataQueryOptions<TodoList> odataQueryOptions)

的Fiddler:

请求:

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}