Naturally sorting IQueryable rows in WCF
I have a nice way to sort data "naturally" using IComparer
similar to @ 's answers to Natural Sort Order in C # . In my case, I have a class that looks something like this:
public class Widget
{
public int Id { get; set; }
public string Name { get; set; }
}
If I have a collection that reads like this,
[{Id: 1, Name: "2 N. Street"},{Id: 2, Name: "33 N. Street"},{Id: 3, Name: "4 N. Street"}]
using an OData query like the following will return the data in the same order as above,
http://www.example.com/Widget.svc/GetWidgets?$orderby=Name
while I really would like the data to be "naturally" ordered as such:
[{Id: 1, Name: "2 N. Street"},{Id: 3, Name: "4 N. Street"},{Id: 2, Name: "33 N. Street"}]
Is there a good way to set up a WCF service to make any request that tries to order any string property to sort the data "naturally"?
source to share
This is now possible through a NuGet preload package called Buildium.ComparedQueryable .
Install-Package Buildium.ComparedQueryable -Version 0.1.3
Given what you have IQueryable
stored in memory, calling AsNaturalQueryable
on it will return a naturally sorted collection.
using ComparedQueryable;
public IHttpActionResult Get()
{
IEnumerable<Widget> widgets = this.service.GetWidgets();
return widgets.AsNaturalQueryable();
}
source to share