Nhibernate ICriteria和在查询中使用Lambda表达式

Nhibernate ICriteria和在查询中使用Lambda表达式,nhibernate,hql,icriteria,Nhibernate,Hql,Icriteria,嗨,我是新来的,我有点困惑 假设我们有一个产品表。 让product表有两列price1和price2 然后我可以通过HQL查询映射的产品实体,如下所示: string queryString = @"from product p where p.price1 = p.price2 + 100 "; IList result = session.CreateQuery(queryString).List(); 如何通过ICriteria API实现这一点 我知道这很荒谬,但我正在尝试这样的事情

嗨,我是新来的,我有点困惑

假设我们有一个产品表。 让product表有两列price1和price2

然后我可以通过HQL查询映射的产品实体,如下所示:

string queryString = @"from product p
where p.price1 = p.price2 + 100 ";
IList result = session.CreateQuery(queryString).List();
如何通过ICriteria API实现这一点

我知道这很荒谬,但我正在尝试这样的事情:

session.CreateCriteria(typeof(product))
   .Add(Expression.Eq("price1", "price2" + 100))
   .List()
或者更恰当地说类似的内容(使用lambda扩展):

session.CreateCriteria(产品类型))
.Add(p=>p.price1==(p.price2+100))
.List()
事实上,我从googlecode下载了lambda extensions项目,并对其进行了扩展,以反复处理二进制和一元表达式,从而实现如下表达式:

session.CreateCriteria(typeof(product))
   .Add<product>(p => 
               (!(p.price1 > 29007) && (p.price1 > 19009)) 
            || (p.price2 == 29009));
session.CreateCriteria(产品类型))
.Add(p=>
(!(p.price1>29007)和&(p.price1>19009))
||(p.price2==29009));
我目前正在处理像上面这样的查询,但是算术伦理子句让我很恼火,因为我无法返回适当的限制来获得我需要的标准

我厌倦了试图用一种全面的方式来解释它。我希望它能奏效。非常感谢您的帮助。

您可以尝试以下方法:

ISQLFunction sqlAdd = new VarArgsSQLFunction("(", "+", ")");
var products = session
    .CreateCriteria<product>()
    .Add(
        Expression.EqProperty(
            "price1", 
            Projections.SqlFunction(
                sqlAdd, 
                // TODO: replace this with appropriate type
                NHibernateUtil.Double,
                Projections.Property("price2"),
                Projections.Constant(100) 
            )
        )
    )
    .List<product>();
ISQLFunction sqlAdd=newvarargssqlfunction(“(”,“+”,”);
var产品=会话
.CreateCriteria()
.添加(
表达式.设备属性(
“价格1”,
Projections.SqlFunction(
sqlAdd,
//TODO:将其替换为适当的类型
NHibernateUtil,双倍,
预测:不动产(“价格2”),
预测.常数(100)
)
)
)
.List();

ty非常感谢,那种方言翻译效果非常好。事实上,它还为我提供了处理MethodCallExpression的另一条线索,例如=>session.CreateCriteria(typeof(product)).Add(p=>p.price1.ToString()==(p.price2+100.ToString()).List()。例如,扩展您的方式,我知道可以使用ToString()在类型NUMBER和VARCHAR2的两列之间创建eqProperty条件在表达式中。连续两次。。。
ISQLFunction sqlAdd = new VarArgsSQLFunction("(", "+", ")");
var products = session
    .CreateCriteria<product>()
    .Add(
        Expression.EqProperty(
            "price1", 
            Projections.SqlFunction(
                sqlAdd, 
                // TODO: replace this with appropriate type
                NHibernateUtil.Double,
                Projections.Property("price2"),
                Projections.Constant(100) 
            )
        )
    )
    .List<product>();