2

我想将 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 查询中没有任何类似的内容,但没有不采用该表达式的重载。

4

2 回答 2

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) - 文档中查找“使用别名创建标准关联”

编辑:

您实际上可以更有效地编写如下:

// 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>();
于 2009-09-14T17:58:44.320 回答
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>();

我认为这将在目标处建立一个新标准,并通过目标别名分配一个别名。

于 2009-04-17T21:14:46.387 回答