简体   繁体   中英

Sorting Query Azure Mobile services

I have a Azure mobile service and I would like to sort the result before handing them to the receiving end.

This is the GetTables function :

 // GET tables/BPNews
    public IQueryable<BPNews> GetAllBPNews()
    {
        return Query().OrderByDescending(x => x.DateStart).Where(x => x.DateStart <= DateTime.Now && x.DateEnd >= DateTime.Now);
    }

I would like to have the results sorted by 'DateStart', but whatever property I sort by it's not been sorted at all.

Here is the BPNews class :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.WindowsAzure.Mobile.Service;

namespace BeautyPointGroningenService.DataObjects
{
public class BPNews : EntityData
{
    public string Subject { get; set; }
    public DateTime Date { get; set; }
    public string Message { get; set; }
    public DateTime DateStart { get; set; }
    public DateTime DateEnd { get; set; }
}
}

Hope you guys can help me out.

Thanks in advance and happy holidays!

UPDATE (result) :

[{"id":"02A99F29-859A-4ED1-991F-D9BF1EDD6BDB","dateEnd":"2014-12-19T00:00:00Z","dateStart":"2014-12-12T00:00:00Z","message":"test \\r\\n\\r\\nfadfasdf asdf asdf\\r\\n\\r\\na\\r\\ndsf a\\r\\ns\\r\\nf\\r\\nas\\r\\nf\\r\\nasd\\r\\nf\\r\\nas\\r\\ndfasf\\r\\na\\r\\nadf\\r\\n\\r\\n\\r\\n\\r\\nfff\\r\\nf","date":"2014-12-13T13:02:43.14Z","subject":"test 23"},{"id":"5753D737-621B-4CD7-A80E-A20CF24CE013","dateEnd":"2014-12-30T00:00:00Z","dateStart":"2014-12-12T00:00:00Z","message":"bericht","date":"2014-12-19T12:53:21.91Z","subject":"test"},{"id":"620693A0-DE00-473E-9CB2-A9F69A1CC699","dateEnd":"2014-12-20T00:00:00Z","dateStart":"2014-12-13T00:00:00Z","message":"Dit is een nieuw bericht","date":"2014-12-13T12:52:00Z","subject":"Nieuw bericht"},{"id":"sample string 6","dateEnd":"2014-12-31T18:53:50Z","dateStart":"2014-12-11T18:53:50.487Z","message":"Dan hebben wij een leuke actie voor je! Met de kerst budget behandeling kun je voor een klein prijsje toch tip-top de feestdagen in. \\r\\n\\r\\nU kunt deze behandeling boeken bij Acties.","da te":"2014-12-11T18:53:50.487Z","subject":"testKlein budget deze maand?"},{"id":"sample string 6ff","dateEnd":"2014-12-31T18:53:50Z","dateStart":"2014-12-11T18:53:50.487Z","message":"Tot 1 december kun je gebruik maken van deze knaller! \\r\\nStart voor 1 december met IPL van de onderbenen incl. knie en je betaald slechts €75 per behandeling! (normaal ongeveer €150) Heerlijk toch? \\r\\nVolgende zomer geen gedoe meer met scheren en waxen!","date":"2014-12-11T18:53:50.487Z","subject":"IPL onderbenen nu € 75 pb"}]

If you change your code to:

    // GET tables/BPNews
    public IQueryable<BPNews> GetAllBPNews()
    {
        return Query().Where(x => x.DateStart <= DateTime.Now && x.DateEnd >= DateTime.Now);
    }

...you can specify the sort order on the client by using OData query operations: http://msdn.microsoft.com/en-us/library/azure/jj677199.aspx

So your query could look like:

    tables/BPNews?$orderby=DateStart

By using the Mobile Services SDK it is much more convenient: http://azure.microsoft.com/en-us/documentation/articles/mobile-services-windows-dotnet-how-to-use-client-library/#sorting

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM