【问题标题】:Breeze WebAPI: How to combine QueryResult with ODataQueryOptions to return inlineCountBreeze WebAPI:如何将 QueryResult 与 ODataQueryOptions 结合以返回 inlineCount
【发布时间】:2013-09-23 13:19:46
【问题描述】:

我尝试了使用微风和 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)的第一项中。

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

这可以通过在 TodoLists 方法中添加 ODataQueryOptions 作为参数,在 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}

【问题讨论】:

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


    【解决方案1】:

    解决方案似乎是:

    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}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      • 2014-06-04
      • 2012-05-29
      • 1970-01-01
      相关资源
      最近更新 更多