NHIbernate OR 条件查询 [英] NHIbernate OR Criteria Query

查看:31
本文介绍了NHIbernate OR 条件查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下映射类

Trade { ID, AccountFrom, AccountTo }
Account {ID, Company}
Company {ID}

现在我想不出一种选择所有交易的方法

Now I cannot figure out a way select all trades where

AccountFrom.Company.ID = X OR AccountTo.Company.ID = X

我可以使用以下方法让 AND 工作:

I can get AND to work using the following:

criteria.CreateCriteria("AccountFrom").CreateCriteria("Company").Add(Restrictions.Eq("ID", X);
criteria.CreateCriteria("AccountTo").CreateCriteria("Company").Add(Restrictions.Eq("ID", X);

但是我如何才能将其转换为 OR 而不是 AND.我以前使用过分离,但我似乎不知道如何添加单独的条件,只是限制.

But how can I transform this into an OR rather an an AND. I have used Disjunction previously, but I cannot seem to know how to add separate criteria, just restrictions.

推荐答案

尝试:

return session.CreateCriteria<Trade>()
    .CreateAlias("AccountFrom", "af")
    .CreateAlias("AccountTo", "at")
    .Add(Restrictions.Or(
        Restrictions.Eq("af.Company.CompanyId", companyId), 
        Restrictions.Eq("at.Company.CompanyId", companyId)))
    .List<Trade>();

我认为您不需要为 Company 取别名.

I don't think you will need to alias Company.

这篇关于NHIbernate OR 条件查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆