Linq Order Doesn't Work

The Linq query "order by" does not work, and I followed all the offers found on your site and on other sites. Any help 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 the WebGet method for the WCF data service. I get a 400 error if I do not return the IQueryable type, so I changed your suggestion a bit. Unfortunately, any order is still apparently ignored.

 [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; } 
+8
linq sql-order-by
source share
3 answers

I noticed that you are returning an IQueryable<T> - are you calling any LINQ methods on the result before listing it?

Not all LINQ methods keep order . Most often, calling Distinct() after you place an order will destroy the order.

+19
source share

Since your method is marked with the WebGet attribute, I assume that you are calling this method from the Web endpoint, so you may need to collapse the collection before sending it over the 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; } 

Thus, you have the guarantee of returning the GetChargeProviders method and the array instead of the linq expression.

Hi,

+1
source share

I found the cause of the problem.

I did not set the column "fullname" as the entity key for the data model object "vw_providercharge_providers". Only an identity column was specified as an entity key. I did not understand that it was a requirement to use it in order.

Thanks again for your input.

+1
source share

All Articles