1

I'm a little lost here. I've tried multiple different methods for returning this list of names I have but I can't seem to return them in the correct alphabetical order. This is what I've got:

    [HttpGet]
    [Queryable(PageSize=150)]
    public IQueryable<BoyName> GetBoyNames(string letter)
    {
        List<BoyName> names = Db.BoyNames.Where(c => c.Name.StartsWith(letter)).OrderBy(x => x.Name).ToList();
        return names.AsQueryable();
    }

And my model:

public partial class BoyName
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Meaning { get; set; }
    public string Origin { get; set; }

}

It is giving me names which is nice...but I can't seem to get them to display in order.

Matthew The Terrible
  • 1,589
  • 5
  • 31
  • 53
  • 5
    Perhaps the problem is with how you are displaying them because the way you've written it should work. On a side note, is there a reason you're doing a `ToList()` to turn the `IQueryable` into an `IEnumerable` and then turning around and turning it back into an `IQueryable`? – Craig W. Apr 17 '14 at 20:26
  • 1
    I'm guessing the issue is with case sensitivity? Saying it isn't displaying "in order" isn't a ton of detail. [Using a custom comparator in OrderBy](http://stackoverflow.com/questions/5328737/linq-and-case-sensitivity). – Nathan Apr 17 '14 at 20:28
  • 2
    oh yeah just grasping at straws. Its the AsQueryable is destroying the odering I think. If I just return a list and do orderby the names come out as expected. – Matthew The Terrible Apr 17 '14 at 20:28
  • 1
    @MatthewTheTerrible is right, if you use `.OrderBy(x => x.Name)` and convert it to a list, and then to a queryable, it is going to do what it wants with the order, if you want it to stay in order, you should probably just return the IOrderedQueryable object – tophallen Apr 17 '14 at 20:32
  • 1
    I suggest you set a variable to `names.AsQueryable()` and then debug and check the order of the `names` variable and the new one right there in VS. – Jeremy Cook Apr 17 '14 at 20:35
  • 1
    The conversion `.ToList()` isn't needed and will probably side-stop your problem if indeed it is a problem on the server side. – Jeremy Cook Apr 17 '14 at 20:37
  • Oh wait, is this a bad solution? Just call that function but add my own parameters to orderby like /api/name/getboynames?$orderby=name&letter=A or something? – Matthew The Terrible Apr 17 '14 at 22:03
  • 1
    It should work with the $orderby in the OData call. What is happening is by default OData queries are order by their ID. So after you have ordered it in your method the Web API OData handler adds it's orderby of ID which overwrites your order by. But if it's passed in on the URL then Web API OData handler will auto order by the URL parameter and not the ID. – CharlesNRice Apr 20 '14 at 01:29

1 Answers1

3

As pointed out in the comments above, calling AsQueryable() on your list object may not preserve the ordering of the list. Rather than converting the result of your LINQ query to a List and then calling AsQueryable, just return the result of your LINQ query since it is a valid return type for your method (IQueryable<BoyName>).

var names = Db.BoyNames
              .Where(c => c.Name.StartsWith(letter))
              .OrderBy(x => x.Name);
return names;
Troy Carlson
  • 2,965
  • 19
  • 26