views:

126

answers:

2

I have a query hitting EF4 using STEs and I'm having an issue with user-defined sorting. In debugging this, I have removed the dynamic sorting and am hard-coding it and I still have the issue. If I swap/uncomment the var results = xxx lines in GetMyBusinesses(), my results are not sorted any differently - they are always sorting it ascendingly.

FYI, Name is a varchar(200) field in SQL 2008 on my Business table.

private IQueryable<Business> GetMyBusinesses(MyDBContext CurrentContext)
{

    var myBusinesses = from a in CurrentContext.A
                       join f in CurrentContext.F
                           on a.FID equals f.id
                       join b in CurrentContext.Businesses
                           on f.BID equals b.id
                       where a.PersonID == 52
                       select b;

    var results = from r in myBusinesses
              orderby "Name" ascending
              select r;

    //var results = from r in results
    //          orderby "Name" descending
    //          select r;

    return results;
}

private PartialEntitiesList<Business> DoStuff()
{
    var myBusinesses = GetMyBusinesses();
    var myBusinessesCount = GetMyBusinesses().Count();

    Results = new PartialEntitiesList<Business>(myBusinesses.Skip((PageNumber - 1)*PageSize).Take(PageSize).ToList())
                  {UnpartialTotalCount = myBusinessesCount};

    return Results;
}

public class PartialEntitiesList<T> : List<T>
{
    public PartialEntitiesList()
    {
    }

    public PartialEntitiesList(int capacity) : base(capacity)
    {
    }

    public PartialEntitiesList(IEnumerable<T> collection) : base(collection)
    {
    }

    public int UnpartialTotalCount { get; set; }
}
A: 

Apparently having the OrderBy clause as a string is invalid (I thought I had tested that before?!). If I change my Linq query to the following, then it works:

var results = from r in myBusinesses
          orderby r.Name ascending
          select r;

This doesn't solve my problem but it does answer this specific question. I'll be posting another question related to my problem.

Jaxidian
It's not invalid; it's just not what you expect. `orderby "Name"` compiles to `.OrderBy("Name")` which expects ESQL. `"Name"` isn't valid, but `"it.Name"` is. So `orderby "it.Name"` might actually work.
Craig Stuntz
@Craig: I have actually tried what would be the equivalent of `"it." + "Name"` long ago and had no luck with that. I assume the fact that I'd be adding the strings together wouldn't have affected the validity of it?
Jaxidian
No, that would have no effect. The key insight here is that the language integrated query (`orderby`) and the method call (`.OrderBy`) compile to the same thing.
Craig Stuntz

related questions