nfplee
nfplee

Reputation: 7977

Nhibernate 3 - LINQ Issues

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.

Upvotes: 1

Views: 2234

Answers (3)

Aim Kai
Aim Kai

Reputation: 2909

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

Upvotes: 1

nfplee
nfplee

Reputation: 7977

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.

Upvotes: 2

Jos&#233; F. Romaniello
Jos&#233; F. Romaniello

Reputation: 14156

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

Upvotes: 2

Related Questions