问题描述
我试着加载使用微风和API控制器项目的列表,使用过滤器(部分使用自定义的对象,并使用ODataQueryOptions另一部分)的方式不同,但他们没有变成是真正的成功。
测试code在JavaScript:
函数test(){
EntityQuery
。从(产品)
/ *
.withParameters({
过滤器:
{列:名称,值:12}
]})
* /
.orderBy(名称降序)
.skip(30)
。取(15)
.inlineCount(真)
。采用(经理)
。执行()
。然后(成功)
.fail(失败); 函数成功(数据){
的console.log(data.products);
的console.log(data.inlineCount);
}
功能失效(错误){
的console.log(返回Error.message);
}
};
测试();
我非常希望做到这一点使用是这样的:
公开的IQueryable<产品与GT;产品([FromUri] FilterObject []过滤器,ODataQueryOptions odataQueryOptions)
{
VAR的结果= DummyData.GetProducts();
//变种totalRowCount = result.Count(); 返回结果;
}
的数据将被其他地方过滤(使用NHibernate),我去掉用于分析过滤器等,但是,这将行不通,因为其它层将返回的总的行数的部分。
所以,我试图来取代它:
公共QueryResult中产品([FromUri] FilterObject []过滤器,ODataQueryOptions odataQueryOptions)
{
...
返回新QueryResult中
{
InlineCount = totalRowCount,
结果=结果
};
}
这将引发一个错误:
不能创建一个EDM模型作为控制器产品的动作'产品'的返回类型Breeze.WebApi.QueryResult没有实现IEnumerable。
删除ODataQueryOptions变量时错误消失。一个搜索没有给我宝贵的意见。
我试过这样:
公共PageResult<产品与GT;产品([FromUri] FilterObject []过滤器,ODataQueryOptions odataQueryOptions)
{
....
返回新PageResult<产品及GT;(因此,空,totalRowCount);
}
这会不会引发错误。当打开返回的数据对象包含与值是未定义的inlineCount参数,实际数据是在第一项中的嵌套的结果阵列(计数,相关文件和NextPageLink)
这是得到这个工作的唯一途径?
这可以轻而易举的NoDb样品中被复制,通过添加ODataQueryOptions作为参数传递给TodoLists方法:
//获取〜/微风/ BreezeTodo / TodoList的
[HTTPGET]
公众的IQueryable<&TodoList的GT; TodoLists(ODataQueryOptions odataQueryOptions)
//公共的IQueryable<&TodoList的GT; TodoLists()
{
VAR的结果= _repository.TodoLists;
结果= result.OrderByDescending(T => t.TodoListId); 返回结果;
}
使用
收益breeze.EntityQuery
。从(TodoLists)
.inlineCount(真)
.skip(0)。取(15)
。采用(经理).execute()
。然后(getSucceeded)
.fail(getFailed);
请求如下:
GET /微风/藤/ TodoLists $顶部= 15安培; $ inlinecount =所有页HTTP / 1.1
与ODataQueryOptions的小提琴结果是:
[{的$ id:1,$类型:NoDb.Models.TodoList,NoDb,TodoListId:1,标题:之前工作,托多斯:[{$标识:2,$类型:NoDb.Models.TodoItem,NoDb,TodoItemId:1,标题:让coffee\",\"IsDone\":false,\"TodoListId\":1,\"TodoList\":{\"$ref\":\"1\"}},{\"$id\":\"3\",\"$type\":\"NoDb.Models.TodoItem, NoDb,TodoItemId:2,标题:打开加热器关,IsDone:假的,TodoListId:1,TodoList的:{$ REF:1}}]}]
和无:
{的$ id:1,$类型:Breeze.WebApi.QueryResult,Breeze.WebApi,结果:[{$标识: 2,$类型:NoDb.Models.TodoList,NoDb,TodoListId:1,标题:工作之前,托多斯:[{$标识:3, $类型:NoDb.Models.TodoItem,NoDb,TodoItemId:1,标题:让coffee\",\"IsDone\":false,\"TodoListId\":1,\"TodoList\":{\"$ref\":\"2\"}},{\"$id\":\"4\",\"$type\":\"NoDb.Models.TodoItem, NoDb,TodoItemId:2,标题:打开加热器关闭,IsDone:假的,TodoListId:1,TodoList的:{$ REF:2}}]}], InlineCount:1}
解决的办法似乎是:
公共QueryResult中TodoLists(ODataQueryOptions<&TodoList的GT; odataQueryOptions)
菲德勒:
请求:
GET /微风/藤/ TodoLists $顶部= 15安培; $ inlinecount =所有页HTTP / 1.1
响应:
{的$ id:1,$类型:Breeze.WebApi.QueryResult,Breeze.WebApi,结果:[{$标识: 2,$类型:NoDb.Models.TodoList,NoDb,TodoListId:1,标题:工作之前,托多斯:[{$标识:3, $类型:NoDb.Models.TodoItem,NoDb,TodoItemId:1,标题:让coffee\",\"IsDone\":false,\"TodoListId\":1,\"TodoList\":{\"$ref\":\"2\"}},{\"$id\":\"4\",\"$type\":\"NoDb.Models.TodoItem, NoDb,TodoItemId:2,标题:打开加热器关闭,IsDone:假的,TodoListId:1,TodoList的:{$ REF:2}}]}], InlineCount:1}
I tried different ways of loading a list of items using breeze and the API Controller, using filters (partly using a custom object and another part using ODataQueryOptions), but none of them turn out to be really successful.
Test code in 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();
Ideally I would like to accomplish this using something like this:
public IQueryable<Product> Products([FromUri] FilterObject[] filters, ODataQueryOptions odataQueryOptions)
{
var result = DummyData.GetProducts();
//var totalRowCount = result.Count();
return result;
}
The data will be filtered somewhere else (using nHibernate), I removed the part used to parse filters etc. However, this will never work since the other layer will return the total row count.
So I tried to replace it with :
public QueryResult Products([FromUri] FilterObject[] filters, ODataQueryOptions odataQueryOptions)
{
...
return new QueryResult
{
InlineCount = totalRowCount,
Results = result
};
}
This throws an error:Cannot create an EDM model as the action 'Products' on controller 'Product' has a return type 'Breeze.WebApi.QueryResult' that does not implement IEnumerable.
The error disappears when removing the ODataQueryOptions variable. A search didn't give me valuable feedback.
I tried this:
public PageResult<Product> Products([FromUri] FilterObject[] filters, ODataQueryOptions odataQueryOptions)
{
....
return new PageResult<Product>(result, null, totalRowCount);
}
This won't throw an error. When opening the returned data object contains an inlineCount parameter with value being undefined, the actual data is in the first item in a nested results array (Count, Items and NextPageLink).
Is this the only way to get this working?
This can be reproduced in the NoDb sample of breeze, by adding ODataQueryOptions as parameter to the TodoLists method:
// 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;
}
Using
return breeze.EntityQuery
.from("TodoLists")
.inlineCount(true)
.skip(0).take(15)
.using(manager).execute()
.then(getSucceeded)
.fail(getFailed);
The request looks like this:
GET /breeze/Todo/TodoLists?$top=15&$inlinecount=allpages HTTP/1.1
The fiddle result with the 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"}}]}]
And without:
{"$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}
The solution appears to be:
public QueryResult TodoLists(ODataQueryOptions<TodoList> odataQueryOptions)
Fiddler:
Request:
GET /breeze/Todo/TodoLists?$top=15&$inlinecount=allpages HTTP/1.1
Response:
{"$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}
这篇关于微风的WebAPI:如何与QueryResult中以ODataQueryOptions返回inlineCount结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!