locked
Problems with WCF Data Service OData Sorting RRS feed

  • Question

  • Couple questions.

    1.  If I don't put an $orderby= on my OData url, the data seems to get re-ordered from the natural list that I created.  That is, I created a simple List<RecordDef> in my service, and returned it as IQueriable().  The list is getting populated in a totally different order.

    2.  When I try and sort by a date column, that is using the call below, if I specify orderby=Id, it works correctly. If I specify CreateDate (which is in my JSON as specified below), it does not work.  It gives the error below.

    'WcfDataServiceTwitter.svc/TweetsStatusInfo?$expand=TweetUserInfo&$inlinecount=allpages&$top=25&$orderby=CreateDate

    {
    "error": {
    "code": "", "message": {
    "lang": "en-US", "value": "No property \'CreateDate\' exists in type \'TweetOData.TweetStatusInfo\' at position 0."
    }
    }
    }

    My JSON going to the client is this:

    {
          "d": {
                "results": [
                      {
                            "__metadata": {
                                  "uri": "http://localhost.:2706/WcfDataServiceTwitter.svc/TweetsStatusInfo(11349406170L)", 
                                  "type": "TweetOData.TweetStatusInfo"
                            }, 
                            "TweetUserInfo": {
                                  "__metadata": {
                                        "uri": "http://localhost.:2706/WcfDataServiceTwitter.svc/TweetsUserInfo(14660297)", 
                                        "type": "TweetOData.TweetUserInfo"
                                  }, 
                                  "Id": 14660297, 
                                  "ProfileImageUrl": "http://a3.twimg.com/profile_images/765427875/jp_head_shot_normal.png", 
                                  "CreateDate": "/Date(1209999600000)/", 
                                  "ScreenName": "John_Papa", 
                                  "RealName": "John Papa", 
                                  "IsFollowing": null, 
                                  "FollowersCount": 1974, 
                                  "FriendsCount": 272, 
                                  "IsProtected": false, 
                                  "Description": "Silverlight Evangelist for Microsoft. Speaker, developer, author, columnist, Silverlight fiend, data guy, husband and father.", 
                                  "Url": "http://johnpapa.net", 
                                  "TimeZone": "Eastern Time (US & Canada)", 
                                  "StatusesCount": 7224, 
                                  "StatusText": null
                            }, 
                            "Id": "11349406170", 
                            "CreatedDate": "/Date(1270010043000)/", 
                            "InReplyToScreenName": null, 
                            "InReplyToStatusId": "0", 
                            "InReplyToUserId": 0, 
                            "IsFavorited": false, 
    

     

     


    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    Wednesday, March 31, 2010 5:05 AM

Answers

  • Hi,

    #1 - We don't enforce/retain any ordering unless you ask for it. This is to allow providers to work as they usually do (if the underlying storage is a DB some queries might change the order cause that's how DB works and adding orderby on everything can be expensive). That said this usually works, especially if you're using List<T> as your provider. What kind of query does not maintain the order for you? (not that you should rely on it anyway)

    #2 - I think the error message is clear enough - property called "CreateDate" is not defined on the type in question. From the JSON payload, it seems that your property is called "CreatedDate" (note the additional "d" in it).

    Thanks,


    Vitek Karas [MSFT]
    • Marked as answer by Peter Kellner Wednesday, March 31, 2010 1:17 PM
    • Marked as answer by Peter Kellner Wednesday, March 31, 2010 1:18 PM
    • Unmarked as answer by Peter Kellner Wednesday, March 31, 2010 1:18 PM
    • Marked as answer by Peter Kellner Wednesday, March 31, 2010 1:18 PM
    • Marked as answer by Peter Kellner Wednesday, March 31, 2010 1:18 PM
    Wednesday, March 31, 2010 6:28 AM
    Moderator

All replies

  • Hi,

    #1 - We don't enforce/retain any ordering unless you ask for it. This is to allow providers to work as they usually do (if the underlying storage is a DB some queries might change the order cause that's how DB works and adding orderby on everything can be expensive). That said this usually works, especially if you're using List<T> as your provider. What kind of query does not maintain the order for you? (not that you should rely on it anyway)

    #2 - I think the error message is clear enough - property called "CreateDate" is not defined on the type in question. From the JSON payload, it seems that your property is called "CreatedDate" (note the additional "d" in it).

    Thanks,


    Vitek Karas [MSFT]
    • Marked as answer by Peter Kellner Wednesday, March 31, 2010 1:17 PM
    • Marked as answer by Peter Kellner Wednesday, March 31, 2010 1:18 PM
    • Unmarked as answer by Peter Kellner Wednesday, March 31, 2010 1:18 PM
    • Marked as answer by Peter Kellner Wednesday, March 31, 2010 1:18 PM
    • Marked as answer by Peter Kellner Wednesday, March 31, 2010 1:18 PM
    Wednesday, March 31, 2010 6:28 AM
    Moderator
  • Thanks.

    On #1, it seems to sort by the Primary Key by default.  I'll verify later on a simple example, but I'm pretty sure it ignores the original order.  It's just not intuitive and it cost me a lot of time before I realized that.

    On #2, you are right.  I thought I had fixed that but somehow the typo had gotten by me.  

    I'm good now.  I appreciate your help.


    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    Wednesday, March 31, 2010 1:17 PM
  • Hi,

    Just a note on #1. If you have server driven paging turned on for that entity set, then it will sort by key properties always. That is needed to get the server driven paging to work correctly. If you don't have SDP on, it should not force any ordering and in your case I would expect the original order. But again, you should not rely on the order unless you specifically asked for it.

    Thanks,


    Vitek Karas [MSFT]
    Wednesday, March 31, 2010 3:52 PM
    Moderator
  • Thanks again Vitek.  I'm not sure about server driven pages, but by default, if you use $top, sorting happens.  The documentation (to me) is not 100% clear about that.  I personally think it should be stated explicitly.

    At anyrate, I just blogged an example that shows the behavior and adds a little discussion here:

    http://peterkellner.net/2010/03/31/odata-wcfdataservice-top-orderby-default-sort/


    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    Wednesday, March 31, 2010 6:38 PM
  • Thanks a lot Peter!

    Just coping the information from your blog, so that people don't need to search for it (until we fix MSDN).

    WCF Data Services server, if the request doesn't include $orderby query option will force sorting by keys on an entity set if:

    - The request included either $top or $skip query options - this is so that client can use these query options to page through the data on its own, so we need stable order of the entities in the set.

    - The request is for entity set which has Server Driven Paging turned on (config.SetEntitySetPageSize). In this case the server will allow paging through the data, so it also needs a stable order of the entities in the set.

    Thanks,


    Vitek Karas [MSFT]
    Wednesday, March 31, 2010 8:37 PM
    Moderator