C# 4.0 重建ODataQueryOptions对象并返回null的GetInlineCount

C# 4.0 重建ODataQueryOptions对象并返回null的GetInlineCount,c#-4.0,asp.net-web-api,odata,C# 4.0,Asp.net Web Api,Odata,在返回PageResult的ODataWebAPI调用中,我从方法参数中提取requestUri,操作过滤项,然后使用新uri构造新的ODataQueryOptions对象 (PageResult方法基于以下内容: ) 以下是原始入站uri,其中包括%24inlinecount=allpages http://localhost:59459/api/apiOrders/?%24filter=OrderStatusName+eq+'Started'&filterLogic=AND&

在返回PageResult的ODataWebAPI调用中,我从方法参数中提取requestUri,操作过滤项,然后使用新uri构造新的ODataQueryOptions对象

(PageResult方法基于以下内容: )

以下是原始入站uri,其中包括%24inlinecount=allpages

http://localhost:59459/api/apiOrders/?%24filter=OrderStatusName+eq+'Started'&filterLogic=AND&%24skip=0&%24top=10&%24inlinecount=allpages&_=1376341370337
就返回的数据而言,除了Request.GetInLineCount返回null之外,其他一切都正常工作

这将“终止”客户端的分页,因为客户端ui元素不知道记录的总数

我构造新ODataQueryOptions对象的方式一定有问题

请看下面我的代码。任何帮助都将不胜感激

我怀疑这篇文章可能包含一些线索,但我被难住了

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

这就消除了foreach循环

只有当客户端通过查询选项请求InlineCount时,InlineCount才会出现

在修改uri逻辑中,添加查询选项
$inlinecount=allpages
,如果该选项尚未出现

此外,代码中还有一个小错误。您正在创建的新ODataQueryOptions使用新的
请求
,其中与GetInlineCount调用一样,您使用的是旧的
请求
。它们不一样

应该是,

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

$inlinecount=所有页面都存在于原始和修改后的URI中。您正在对
请求
应用查询,并尝试从
请求
读取inlinecount。局部变量vs类属性。我已经更新了答案,也包括了这一点。谢谢@RaghuRamNadiminti。使用局部变量
request
而不是类
request
解决了问题。同样的解决方案适用于
GetNextPageLink()
它应该是
request.GetNextPageLink()
,而不是
request.GetNextPageLink()
除了查询\分页之外,您是否使用OData支持?如果不是,那么考虑使用LINQ查询字符串作为Web API提供的替代方案,正如在这个问题的答案中一样:在Web API API数据的支持下,不支持对DTOS的投影,这样您可能会看到一些不可预测的行为,并且粘贴的代码具有许多不必要的复杂性。谢谢@roysvork我会查一查的。我在客户端使用infragistics igniteui网格和数据源。看起来Linq to Querystring可以正常工作。我会回来报告的。我发现Infragistics igniteui网格使用LinqToQueryable属性使用web api方法中的数据。但是,我希望能够操纵查询服务器端来处理枚举上的过滤和排序,其中客户端显示枚举的字符串表示形式。或者,我相信有更好的方法来处理枚举。
[JsonConverter(typeof(StringEnumConverter))]
public OrderStatus OrderStatusId { get; set; }
var count = request.GetInlineCount(); // use the new request that your created, as that is what you applied the query to.