我想将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 =&gt; g.Department”有关,并且原始NHibernate查询中没有任何内容具有相似之处,但没有任何重载都没有表达。
答案 0 :(得分:3)
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>();
答案 1 :(得分:0)
我没有使用NHLambdaExpressions(但它看起来很酷,我很快就会把它检出来),所以我只是猜测一下。你能做这样的事吗:
Business.Goal goalAlias = null;
var departments = DepartmentService
.CreateCriteria()
.CreateCriteria((Business.Department g) => g.Goals, () => goalAlias)
.Add<Business.Goal>( g => g.Company.Id == companyId )
.Add<Business.Goal>( g => g.Program.Id == programId )
.List<Business.Department>();
我认为这将在Goals中生成一个新标准,并通过goalAlias分配一个别名。