开发者

Using OrderBy with custom IComparer with SubSonic

开发者 https://www.devze.com 2022-12-11 03:17 出处:网络
I am trying to call OrderBy() using a custom IComparer on a SubSonic IQueryable like so: IQueryable<FooObject>开发者_如何学Python sortedFoos =

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

IQueryable<FooObject>开发者_如何学Python 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号