gpt4 book ai didi

odata - Breeze WebAPI : How to combine QueryResult with ODataQueryOptions to return inlineCount

转载 作者:行者123 更新时间:2023-12-04 04:41:10 27 4
gpt4 key购买 nike

我尝试了使用 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 模型,因为 Controller “产品”上的操作“产品”具有未实现 IEnumerable 的返回类型“Breeze.WebApi.QueryResult”。

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

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

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

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

通过将 ODataQueryOptions 作为参数添加到 TodoLists 方法,可以在 Breeze 的 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 的 fiddle 结果:
[{"$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}

最佳答案

解决方案似乎是:

public QueryResult TodoLists(ODataQueryOptions<TodoList> odataQueryOptions)

fiddle 手:

要求:
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}

关于odata - Breeze WebAPI : How to combine QueryResult with ODataQueryOptions to return inlineCount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18878035/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com