如何使用 NHibernate 中的外键引用 属性 of table 过滤结果

How to get result filtered using property of table reference by foreign key in NHibernate

我正在使用 NHibernate 从 SQL 服务器获取数据。

为了获取我写的数据var result = Repository.QueryOver<table_reference>().Where(c => c.Amount >100).List()

现在我想通过外键引用过滤结果,例如

Repository.QueryOver<TaxInvoicePassing>().Where(c => c.Branch.Employee.Salary > 10000).List()

我怎样才能做到这一点?

一种方法是迭代每个 table 的记录,然后添加结果范围

提前致谢

使用QueryOver,你可以试试这个:

// declare the alias
Branch branch = null;
Employee employee = null;

Session.QueryOver<TaxInvoicePassing>() // get a queryOver of TaxInvoicePassing
       .JoinAlias(t => t.Branch, () => branch) // add a join with Branch
       .JoinAlias(() => branch.Employee, () => employee) // add a join with Employee
       .Where(c => employee.Salary > 10000) // add where filter on employee's salary
       .List();

使用Linq,可以像您所问的表达式那样完成:

Session.Query<TaxInvoicePassing>()
       .Where(c => c.Branch.Employee.Salary > 10000) // this expression will be converted on a join
       .List();

我会将这些查询移到存储库对象中,而不是公开它。