重构ODataQueryOptions对象,GetInlineCount返回null [英] Reconstructing an ODataQueryOptions object and GetInlineCount returning null

查看:339
本文介绍了重构ODataQueryOptions对象,GetInlineCount返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个ODATA的WebAPI调用返回一个PageResult我从方法参数提取requestUri,操纵过滤条件,然后构造使用新的URI的新ODataQueryOptions对象。

(将PageResult方法是基于对这个职位:
<一href=\"http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options\" rel=\"nofollow\">http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options )

下面是原始入站URI,其中包括%24inlinecount =所有页

<$p$p><$c$c>http://localhost:59459/api/apiOrders/?%24filter=OrderStatusName+eq+'Started'&filterLogic=AND&%24skip=0&%24top=10&%24inlinecount=allpages&_=1376341370337

一切正常,在数据方面,除了返回Request.GetInLineCount返回null。

此杀死在客户端的客户端UI元素寻呼不知道的记录的总数。

有一定有什么毛病我怎么建造新ODataQueryOptions对象。

请看看我下面code。任何帮助将是AP preciated。

我怀疑这个帖子可能包含一些线索 http://stackoverflow.com/a/16361875/1433194 但我M难倒。

 公共PageResult&LT; OrderVm&GT;获取(ODataQueryOptions&LT; OrderVm&gt;选项)
    {        VAR incomingUri = options.Request.RequestUri.AbsoluteUri;//此处操纵URI,以适应实体模型
//(与所需要的可枚举类型OrderStatusId变换)
//例如。查询字符串可以包含%24filter = OrderStatusName + EQ +'开始'
//我操纵这%24filter = OrderStatusId + EQ +'开始'        ODataQueryOptions&LT; OrderVm&GT;选项​​2;        VAR newUri = incomingUri; // pretend有人操纵如上        //重构与改性乌里的ODataQueryOptions        VAR要求=新的Htt prequestMessage(HttpMethod.Get,newUri);        //构造使用新的请求对象一个新的选择对象
        选项​​2 =新ODataQueryOptions&LT; OrderVm&GT;(options.Context,请求);        //提取从资源库中可查询。内容是一个IQueryable&LT;排序&gt;
        VAR内容= _unitOfWork.OrderRepository.Get(NULL,O =&GT; o.OrderByDescending(C =&GT; c.OrderId),);        //项目它到视图模型在网格被用于显示目的
        //下面的预测等,做工精细,并且不与GetInlineCount如果干扰
        //我避免构造和使用新对象的选项的步骤
        变种DS = contents.Select(O =&gt;新建OrderVm
        {
            的OrderId = o.OrderId,
            为了code = o.Order code,
            客户ID = o.CustomerId,
            AmountCharged = o.AmountCharged,
            客户名称= o.Customer.FirstName ++ o.Customer.LastName,
            捐赠= o.Donation,
            订购日期= o.OrderDate,
            OrderStatusId = o.StatusId,
            OrderStatusName =
        });        //注意使用'选项2'在这里取代了原有的选项
        VAR设置=新ODataQuerySettings()
        {
            每页= options2.Top!= NULL? options2.Top.Value:5
        };        //应用ODATA改造
        //注意使用'选项2'在这里取代了原有的选项
        IQueryable的结果= options2.ApplyTo(DS,设置);        //更新包含字符串再枚举的presentation领域
        的foreach(在结果OrderVm行)
        {
            row.OrderStatusName = row.OrderStatusId.ToString();
        }        //获取结果集中的记录总数
        //当使用'选项2'对象则返回null - 这是我的问题
        变种数= Request.GetInlineCount();        //创建PageResult对象
        VAR PR =新PageResult&LT; OrderVm&GT;(
            结果为IEnumerable&LT; OrderVm&gt;中
            Request.GetNextPageLink(),
            计数
            );
        返回公关;
    }

编辑结果
因此,纠正code应改为

  //创建PageResult对象
VAR PR =新PageResult&LT; OrderVm&GT;(
    结果为IEnumerable&LT; OrderVm&gt;中
    request.GetNextPageLink(),
    request.GetInlineCount();
    );
返回公关;

编辑结果
通过将一个JSON转换到OrderVm类的OrderStatusId财产(一个枚举)

避免在控制器方法枚举的字符串变换的需要

  [JsonConverter(typeof运算(StringEnumConverter))]
公共OrderStatus OrderStatusId {搞定;组; }

这摒弃了foreach循环。


解决方案

InlineCount将是present只有当客户端通过的 $ inlinecount 查询选项。

在你修改URI逻辑添加查询选项 $ inlinecount =所有页如果尚未present。

此外,还有在你的code一个小错误。要创建新的ODataQueryOptions采用了新的要求,其中作为GetInlineCount通话时,您使用的是旧的请求。它们是不相同的。

这是应该的,

 变种数= request.GetInlineCount(); //使用您创建的新的要求,因为这是你用的查询什么。

In an odata webapi call which returns a PageResult I extract the requestUri from the method parameter, manipulate the filter terms and then construct a new ODataQueryOptions object using the new uri.

(The PageResult methodology is based on this post: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options )

Here is the raw inbound uri which includes %24inlinecount=allpages

http://localhost:59459/api/apiOrders/?%24filter=OrderStatusName+eq+'Started'&filterLogic=AND&%24skip=0&%24top=10&%24inlinecount=allpages&_=1376341370337

Everything works fine in terms of the data returned except Request.GetInLineCount returns null.

This 'kills' paging on the client side as the client ui elements don't know the total number of records.

There must be something wrong with how I'm constructing the new ODataQueryOptions object.

Please see my code below. Any help would be appreciated.

I suspect this post may contain some clues http://stackoverflow.com/a/16361875/1433194 but I'm stumped.

public PageResult<OrderVm> Get(ODataQueryOptions<OrderVm> options)
    {

        var incomingUri = options.Request.RequestUri.AbsoluteUri;

//manipulate the uri here to suit the entity model   
//(related to a transformation needed for enumerable type OrderStatusId )
//e.g. the query string may include %24filter=OrderStatusName+eq+'Started' 
//I manipulate this to %24filter=OrderStatusId+eq+'Started'

        ODataQueryOptions<OrderVm> options2;

        var newUri = incomingUri;  //pretend it was manipulated as above

        //Reconstruct the ODataQueryOptions with the modified Uri

        var request = new HttpRequestMessage(HttpMethod.Get, newUri);

        //construct a new options object using the new request object
        options2 = new ODataQueryOptions<OrderVm>(options.Context, request);

        //Extract a queryable from the repository.  contents is an IQueryable<Order>
        var contents = _unitOfWork.OrderRepository.Get(null, o => o.OrderByDescending(c => c.OrderId), "");

        //project it onto the view model to be used in a grid for display purposes
        //the following projections etc work fine and do not interfere with GetInlineCount if
        //I avoid the step of constructing and using a new options object
        var ds = contents.Select(o => new OrderVm
        {
            OrderId = o.OrderId,
            OrderCode = o.OrderCode,
            CustomerId = o.CustomerId,
            AmountCharged = o.AmountCharged,
            CustomerName = o.Customer.FirstName + " " + o.Customer.LastName,
            Donation = o.Donation,
            OrderDate = o.OrderDate,
            OrderStatusId = o.StatusId,
            OrderStatusName = ""
        });

        //note the use of 'options2' here replacing the original 'options'
        var settings = new ODataQuerySettings()
        {
            PageSize = options2.Top != null ? options2.Top.Value : 5
        };

        //apply the odata transformation
        //note the use of 'options2' here replacing the original 'options'    
        IQueryable results = options2.ApplyTo(ds, settings);

        //Update the field containing the string representation of the enum
        foreach (OrderVm row in results)
        {
            row.OrderStatusName = row.OrderStatusId.ToString();
        }

        //get the total number of records in the result set 
        //THIS RETURNS NULL WHEN USING the 'options2' object - THIS IS MY PROBLEM
        var count = Request.GetInlineCount();

        //create the PageResult object
        var pr = new PageResult<OrderVm>(
            results as IEnumerable<OrderVm>,
            Request.GetNextPageLink(),
            count
            );
        return pr;
    }

EDIT
So the corrected code should read

//create the PageResult object
var pr = new PageResult<OrderVm>(
    results as IEnumerable<OrderVm>,
    request.GetNextPageLink(),
    request.GetInlineCount();
    );
return pr;

EDIT
Avoided the need for a string transformation of the enum in the controller method by applying a Json transformation to the OrderStatusId property (an enum) of the OrderVm class

[JsonConverter(typeof(StringEnumConverter))]
public OrderStatus OrderStatusId { get; set; }

This does away with the foreach loop.

解决方案

InlineCount would be present only when the client asks for it through the $inlinecount query option.

In your modify uri logic add the query option $inlinecount=allpages if it is not already present.

Also, there is a minor bug in your code. The new ODataQueryOptions you are creating uses a new request where as in the GetInlineCount call, you are using the old Request. They are not the same.

It should be,

var count = request.GetInlineCount(); // use the new request that your created, as that is what you applied the query to.

这篇关于重构ODataQueryOptions对象,GetInlineCount返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆