NHibernate哪里有别名的限制

本文关键字:别名 NHibernate | 更新日期: 2023-09-27 18:27:11

我正在尝试执行一个查询,其中someProperty.contains(string) || otherProperty.contains(string),因此我找到了以下内容:

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

但是,在此之前我有一个别名,用于连接的属性之一:

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

但我在限制行上遇到了一个错误,别名是:

委托类型不接受0个参数

() => ttypeAlias.TradeTypeName

如何合并此别名?

NHibernate哪里有别名的限制

这是上面使用的.On()的语法:

/// <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);
}

这给了我们答案:

传递的表达式必须有一个参数(不必使用)

将起作用的语法可以是这样的:

// 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)...