Linq 和 QueryOver 采用不同的 sql 路径:
QueryOver -> Expression -> Criteria \
Linq -> LinqParser -> Hql --> Sql
对于标准,有NHibernate.Impl.ExpressionProcessor.RegisterCustomMethodCall(...);
可能是您想要的。
一个简单的例子:
public static class QueryOverExtensions
{
public static void Register()
{
ExpressionProcessor.RegisterCustomProjection(() => QueryOverExtensions.Day(default(DateTime)), QueryOverExtensions.ProcessDay);
ExpressionProcessor.RegisterCustomProjection(() => QueryOverExtensions.Month(default(DateTime)), QueryOverExtensions.ProcessMonth);
ExpressionProcessor.RegisterCustomProjection(() => QueryOverExtensions.Year(default(DateTime)), QueryOverExtensions.ProcessYear);
}
public static Int32 Day(this DateTime dateTimeProperty)
{
return (dateTimeProperty.Day);
}
public static Int32 Month(this DateTime dateTimeProperty)
{
return (dateTimeProperty.Month);
}
public static Int32 Year(this DateTime dateTimeProperty)
{
return (dateTimeProperty.Year);
}
private static IProjection ProcessDay(MethodCallExpression methodCallExpression)
{
IProjection property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
return (Projections.SqlFunction("day", NHibernateUtil.Int32, property));
}
private static IProjection ProcessMonth(MethodCallExpression methodCallExpression)
{
IProjection property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
return (Projections.SqlFunction("month", NHibernateUtil.Int32, property));
}
private static IProjection ProcessYear(MethodCallExpression methodCallExpression)
{
IProjection property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
return (Projections.SqlFunction("year", NHibernateUtil.Int32, property));
}
}
不要忘记调用 Register()。之后,您可以像这样使用它:
session.QueryOver<Order>().Where(o => o.Date.Month() == DateTime.Today.Month).List();