我想将 NHibernate CreateCriteria 转换为 NHLambdaExtensions标准,但我遇到了不知道如何修复的错误。
NHibernate 标准如下所示:
var departments = DepartmentService
.CreateCriteria()
.CreateAlias( "Goals", "goal" )
.Add( Expression.Eq( "goal.Company.Id", companyId ) )
.Add( Expression.Eq( "goal.Program.Id", programId ) )
.List<Business.Department>();
我尝试创建的 NHLambdaExtensions 标准如下所示:
Business.Goal goalAlias = null;
var departments = DepartmentService
.CreateCriteria()
.CreateAlias<Business.Goal>( g => g.Department, () => goalAlias )
.Add<Business.Goal>( g => g.Company.Id == companyId )
.Add<Business.Goal>( g => g.Program.Id == programId )
.List<Business.Department>();
我收到的错误是“无法解析属性部门:Business.Department”。该错误显然与“g => g.Department”有关,原始 NHibernate 查询中没有任何类似的内容,但不存在不采用该表达式的重载。
最佳答案
Business.Goal goalAlias = null;
var departments = DepartmentService
.CreateCriteria(typeof(Business.Department)) // need to specify the first criteria as Business.Department
.CreateCriteria<Business.Department>(d => d.Goals, () => goalAlias)
.Add<Business.Goal>( g => g.Company.Id == companyId )
.Add<Business.Goal>( g => g.Program.Id == programId )
.List<Business.Department>();
在 NHibernate Lambda Extensions (V1.0.0.0) - Documentation 中查找“创建与别名的条件关联”
编辑:
实际上,您可以更有效地将其编写为:
// no alias necessary
var departments = DepartmentService
.CreateCriteria<Business.Department>()
.CreateCriteria<Business.Department>(d => d.Goals)
.Add<Business.Goal>( g => g.Company.Id == companyId )
.Add<Business.Goal>( g => g.Program.Id == programId )
.List<Business.Department>();
关于NHibernate Lambda 扩展 - CreateCriteria 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/761672/