views:

281

answers:

1

I am trying to call OrderBy() using a custom IComparer on a SubSonic IQueryable like so:

IQueryable<FooObject> sortedFoos = 
  FooObject.All()
    .OrderBy(f => f, new FooObjectComparer());

However when I then try to enumerate over sortedFoos or create a PagedList<FooObject> using it, I get a System.Exception: 'The LINQ expression node of type MemberInit is not supported'.

Here is the implementation for FooObjectComparer:

public class FooObjectComparer : IComparer<FooObject>
{
  public FooObjectComparer() {}

  public int Compare(FooObject x, FooObject y)
  {
    return x.MyProperty.CompareTo(y.MyProperty);
  }
}

(This is a simple implementation for debugging purposes. The actual implementation will be more complex).

What am I missing here?

+2  A: 

What you're missing is that SubSonic can't reflect on your comparison operator and figure out how to turn it into SQL. Probably your best bet is to pull the objects first into memory (based on whatever criteria) then order them with your comparer.

Rob Conery