Search code examples
c#nhibernatequeryover

NHibernate where restrictions with an alias


I'm trying to do a query where someProperty.contains(string) || otherProperty.contains(string) so I found the following:

.where(
 restrictions.on<type>(x => x.property).IsLike(string) ||
 restrictions.on<type>(x => x.prop2).IsLike(string)
)

however, I have an alias before this where for one of the properties for a join:

session.QueryOver<Trade>()
   .JoinAlias(x => x.TradeType, () => ttypeAlias)
   .Where(
       Restrictions.On<Trade>(c => c.Nickname).IsLike("%" + searchString + "%") ||
       Restrictions.On<TradeType>(() => ttypeAlias.TradeTypeName).IsLike("%" + searchString + "%") ||
   )

but I'm getting an error on the restriction line with the alias:

delegate type does not accept 0 arguments

() => ttypeAlias.TradeTypeName

how do I incorporate this alias?


Solution

  • This is the syntax of the .On() used above:

    /// <summary>
    /// Build an ICriterion for the given property
    /// </summary>
    /// <param name="expression">lambda expression identifying property</param>
    /// <returns>returns LambdaRestrictionBuilder</returns>
    public static LambdaRestrictionBuilder On<T>(Expression<Func<T, object>> expression)
    {
        ExpressionProcessor.ProjectionInfo projection = ExpressionProcessor.FindMemberProjection(expression.Body);
        return new LambdaRestrictionBuilder(projection);
    }
    

    And that give us the answer:

    passed expression must have one argument (which does not have to be used)

    The syntax, which will work, could be like this:

    // no argument - it is wrong
    // Restrictions.On<TradeType>(() => ttypeAlias.TradeTypeName)...
    
    // here we expect one argument, and naming his lo-dash - is convention to say
    // it won't be used, it is there just to fulfill all the rules
    Restrictions.On<TradeType>(_ => ttypeAlias.TradeTypeName)...