c# 4.0 - Reconstructing an ODataQueryOptions object and GetInlineCount returning null -


in odata webapi call returns pageresult extract requesturi method parameter, manipulate filter terms , construct new odataqueryoptions object using new uri.

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

here raw inbound uri 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 data returned except request.getinlinecount returns null.

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

there must wrong how i'm constructing new odataqueryoptions object.

please see code below. appreciated.

i suspect post may contain clues https://stackoverflow.com/a/16361875/1433194 i'm stumped.

public pageresult<ordervm> get(odataqueryoptions<ordervm> options)     {          var incominguri = options.request.requesturi.absoluteuri;  //manipulate uri here suit entity model    //(related transformation needed enumerable type orderstatusid ) //e.g. query string may include %24filter=orderstatusname+eq+'started'  //i manipulate %24filter=orderstatusid+eq+'started'          odataqueryoptions<ordervm> options2;          var newuri = incominguri;  //pretend manipulated above          //reconstruct odataqueryoptions modified uri          var request = new httprequestmessage(httpmethod.get, newuri);          //construct new options object using new request object         options2 = new odataqueryoptions<ordervm>(options.context, request);          //extract queryable repository.  contents iqueryable<order>         var contents = _unitofwork.orderrepository.get(null, o => o.orderbydescending(c => c.orderid), "");          //project onto view model used in grid display purposes         //the following projections etc work fine , not interfere getinlinecount if         //i avoid step of constructing , using 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 use of 'options2' here replacing original 'options'         var settings = new odataquerysettings()         {             pagesize = options2.top != null ? options2.top.value : 5         };          //apply odata transformation         //note use of 'options2' here replacing original 'options'             iqueryable results = options2.applyto(ds, settings);          //update field containing string representation of enum         foreach (ordervm row in results)         {             row.orderstatusname = row.orderstatusid.tostring();         }          //get total number of records in result set          //this returns null when using 'options2' object - problem         var count = request.getinlinecount();          //create pageresult object         var pr = new pageresult<ordervm>(             results ienumerable<ordervm>,             request.getnextpagelink(),             count             );         return pr;     } 

edit
corrected code should read

//create pageresult object var pr = new pageresult<ordervm>(     results ienumerable<ordervm>,     request.getnextpagelink(),     request.getinlinecount();     ); return pr; 

edit
avoided need string transformation of enum in controller method applying json transformation orderstatusid property (an enum) of ordervm class

[jsonconverter(typeof(stringenumconverter))] public orderstatus orderstatusid { get; set; } 

this away foreach loop.

inlinecount present when client asks through $inlinecount query option.

in modify uri logic add query option $inlinecount=allpages if not present.

also, there minor bug in code. new odataqueryoptions creating uses new request in getinlinecount call, using old request. not same.

it should be,

var count = request.getinlinecount(); // use new request created, applied query to. 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -