1
votes

my application has the following code to add a ToSortedList extension method on any IEnumberable:

public class SortedList<T, TResult> : List<T> {
    public SortedList(IEnumerable<T> source, Expression<Func<T, TResult>> sortBy, SortDirection sortDirection) {
        Initialize(source is IQueryable<T> ? source as IQueryable<T> : source.AsQueryable(), sortBy, sortDirection);
    }

    protected void Initialize(IQueryable<T> source, Expression<Func<T, TResult>> sortBy, System.Web.UI.WebControls.SortDirection sortDirection) {
        AddRange(sortDirection == SortDirection.Ascending ? source.OrderBy(sortBy) : source.OrderByDescending(sortBy));
    }
}

public static class SortingExtensions {
    public static SortedList<T, TResult> ToSortedList<T, TResult>(this IEnumerable<T> source, Expression<Func<T, TResult>> sortBy, SortDirection sortDirection) {
        return new SortedList<T, TResult>(source, sortBy, sortDirection);
    }
}

In the old LINQ provider (on top of NHibernate 2.1) i could then say:

session.Linq<Article>().ToSortedList(a => a.Date, SortDirection.Ascending);

However using the new in-built LINQ provider in NHibernate 3 (change Linq to Query above) this does not work and the following error is thrown:

"Specified method is not supported." - within the Initialize method

I'd really appreciate it if someone could show me how this could be done.

3
can you put the stack trace for where the exception is being thrown from in nhibernate? i have an idea of what the cause is, but i thought it threw a different exception, and usually i only see it w/ extension method used inside query - Darren Kopp

3 Answers

2
votes

In the new provider you should use session.Query(), Linq is an extension method from NHibernate.Linq.dll. You should delete this dll when working with nh3.

So your example should be something like:

session.Query<Article>().ToSortedList(a => a.Date, SortDirection.Ascending);

A side note; you are using SortDirection ffrom WebControls, my advice is to use ListSortDirection from componentmodel http://msdn.microsoft.com/es-es/library/system.componentmodel.listsortdirection(v=VS.80).aspx

2
votes

The trouble i had is that i was doing a Take before an OrderBy which unfortunately threw an exception with the release. I simplified my example and guess i missed the most important part. This has been resolved in a later build.

1
votes

Can't you just use:

var articles =
    session.QueryOver<Article>()
        .OrderBy(a => a.Date).Asc
        .List();

See: http://nhibernate.info/blog/2009/12/17/queryover-in-nh-3-0.html