Hi,

No generator, but try this:

public static IQueryable<TSource> Between<TSource, TKey>(this 
IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, 
TKey low, TKey high) where TKey : IComparable<TKey>
{
// Get a ParameterExpression node of the TSource that is used in the 
expression tree 
var sourceParameter = Expression.Parameter(typeof(TSource));

// Get the body and parameter of the lambda expression 
var body = keySelector.Body;
ParameterExpression parameter = null;

if (keySelector.Parameters.Count > 0)
{
parameter = keySelector.Parameters[0];
}

// Get the Compare method of the type of the return value 
var compareMethod = typeof(TKey).GetMethod("CompareTo", new Type[] { 
typeof(TKey) });

var zero = Expression.Constant(0, typeof(Int32));

// Expression.LessThanOrEqual and Expression.GreaterThanOrEqual method are 
only used in 
// the numeric comparision. If we want to compare the non-numeric type, we 
can't directly  
// use the two methods.  
// So we first use the Compare method to compare the objects, and the 
Compare method  
// will return a int number. Then we can use the LessThanOrEqual and 
GreaterThanOrEqua method. 
// For this reason, we ask all the TKey type implement the IComparable<> 
interface. 
var upper = Expression.LessThanOrEqual(Expression.Call(body, compareMethod, 
Expression.Constant(high)), zero);
var lower = Expression.GreaterThanOrEqual(Expression.Call(body, 
compareMethod, Expression.Constant(low)), zero);

var andExpression = Expression.AndAlso(upper, lower);

// Get the Where method expression. 
var whereCallExpression = Expression.Call
(
typeof(Queryable),
"Where",
new Type[] { source.ElementType },
source.Expression,
Expression.Lambda<Func<TSource, Boolean>>(andExpression, new 
ParameterExpression[] { parameter })
);

return (source.Provider.CreateQuery<TSource>(whereCallExpression));
}


On Wednesday, October 21, 2015 at 3:58:45 PM UTC+1, Jeffrey Becker wrote:
>
> I'm having issues when trying to define an 'IsBetween' extension method 
> per the attached files.  NHibernate ends up throwing the following error 
> and I'm at a loss about it:
>
>
> NHibernate.Hql.Ast.ANTLR.QuerySyntaxExceptionA recognition error occurred. 
> [.Where[GpisReplacement.Domain.Event](NHibernate.Linq.NhQueryable`1[GpisReplacement.Domain.Event],
>  
> Quote((x, ) => (.IsBetween(x.FromDate, p1, p2, ))), )]
>    at NHibernate.Hql.Ast.ANTLR.ErrorCounter.ThrowQueryException()
>    at NHibernate.Hql.Ast.ANTLR.HqlSqlTranslator.Translate()
>    at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.DoCompile(IDictionary`2 
> replacements, Boolean shallow, String collectionRole)
>    at 
> NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IASTNode
>  
> ast, String queryIdentifier, String collectionRole, Boolean shallow, 
> IDictionary`2 filters, ISessionFactoryImplementor factory)
>    at 
> NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression 
> queryExpression, Boolean shallow, IDictionary`2 enabledFilters)
>    at NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression 
> queryExpression, Boolean shallow)
>    at NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression 
> queryExpression)
>    at NHibernate.Linq.DefaultQueryProvider.PrepareQuery(Expression 
> expression, ref IQuery query, ref NhLinqExpression nhQuery)
>    at NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression)
>    at NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression)
>    at Remotion.Linq.QueryableBase`1.GetEnumerator()
>    at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
>    at System.Linq.Enumerable.ToList(IEnumerable`1 source)
>    at GpisReplacement.Test.TestBetween.CanBetween() in TestBetween.cs: 
> line 25
>
>
> The other two extension methods come off without a hitch.
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

Reply via email to