7

The Linq query "order by" is not working and I've followed all the suggestions found on your site and other sites. Any assistance would be appreciated.

    [WebGet]
    public IQueryable<vw_providercharge_providers> GetChargeProviders(int submitted)
    {
        var results = (from p in this.CurrentDataSource.vw_providercharge_providers
                       where p.submitted == submitted
                       orderby p.fullname
                       select p);
        return results;
    }

Thanks for your input!

Yes, this is a WebGet method for a WCF data service. I get a 400 error if I don't return an IQueryable type, so I modified your suggestion a little. Unfortunately, it still seems to disregard any order-by.

[WebGet]
public IQueryable<vw_providercharge_providers> GetChargeProviders(int submitted)
{
    var results = (from p in this.CurrentDataSource.vw_providercharge_providers
                   where p.submitted == submitted
                   orderby p.fullname
                   select p).ToArray();
    results.OrderBy(p => p.patientname);
    return results;
}
Kev
  • 118,037
  • 53
  • 300
  • 385
user680891
  • 177
  • 1
  • 2
  • 12

3 Answers3

18

I notice you return an IQueryable<T> - are you calling any LINQ methods on the result before you enumerate it?

Not all LINQ methods preserve order. Most commonly, calling Distinct() after you do the ordering will destroy the order.

Community
  • 1
  • 1
BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
  • 2
    Thanks for the comment concerning Distinct() that's causing the problem for me. The solution is found here [link](http://stackoverflow.com/questions/12428985/distinct-and-orderby-issue) – mack Oct 03 '14 at 13:30
1

Since your method is a marked with a WebGet attribute, I'm assuming that you are calling this method from a Web endpoint, therefore you may need to collapse the collection prior to send it through internet.

Try:

[WebGet]
public vw_providercharge_providers[] GetChargeProviders(int submitted)
{
    var results = (from p in this.CurrentDataSource.vw_providercharge_providers
                   where p.submitted == submitted
                   orderby p.fullname
                   select p).ToArray();
    return results;
}

This way you have the guarantee that the GetChargeProviders method returns and array instead of an linq expression.

Regards,

wacdany
  • 991
  • 1
  • 10
  • 19
1

I found the cause of the issue.

I had not set the "fullname" column as an Entity Key for the "vw_providercharge_providers" data model entity. Only the identity column was set as an Entity Key. I didn't realize that was a requirement to use it in an order by clause.

Thanks again for your input.

user680891
  • 177
  • 1
  • 2
  • 12
  • 4
    This is not correct. A column does not have to be defined as an Entity Key within the data model for the OrderBy method. I suspect that you are running into some odd behavior due to the manner in which your view is setup in your data model (or rather the incorrect manner). I suspect that having added the "fullname" column as the Entity Key is now forcing your results to order by this column regardless of whether or not you specifically use orderby in your linq query. I order by various columns that are not keys within views in my data context (Entity Framework) and it orders them just fine. – Jagd Feb 01 '12 at 23:19