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.
精彩评论