I found a solution:
Notice the NHibernate (nquery) query that has RegisterCustomAction:
private void CallZipSqlFunction(ListingQuerySpec spec, IQueryable<Listing> query)
{
var nQuery = query as NHibernate.Linq.Query<Listing>;
if (spec.ZipCode.Contains("'"))
throw new SecurityException("invalid character");
var functionString = "dbo.GetDistanceForListing('" + spec.ZipCode + "',{alias}.ID) as Distance";
var distance = Projections.SqlProjection(functionString, new[] { "Distance" }, new IType[] { NHibernateUtil.String });
var filter = Expression.Le(distance, spec.ZipCodeRadius.Value);
nQuery.QueryOptions.RegisterCustomAction(x => x.AddOrder(Order.Asc(distance)));
nQuery.QueryOptions.RegisterCustomAction(x => x.Add(filter));
}
source
share