gpt4 book ai didi

NHibernate Lambda 扩展 - CreateCriteria 问题

转载 作者:行者123 更新时间:2023-12-04 07:08:22 25 4
gpt4 key购买 nike

我想将 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>();

我得到的错误是“无法解析属性(property)部门: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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com