首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NHibernate Lambda扩展- CreateCriteria问题

NHibernate Lambda扩展- CreateCriteria问题
EN

Stack Overflow用户
提问于 2009-04-17 18:37:15
回答 2查看 2.4K关注 0票数 2

我想要将NHibernate CreateCriteria转换为NHLambdaExtensions条件,但我收到了错误,我不知道如何修复。

NHibernate标准如下所示:

代码语言:javascript
复制
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标准如下所示:

代码语言:javascript
复制
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查询中没有类似的内容,但没有不接受该表达式的重载。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-09-14 17:58:44

代码语言:javascript
复制
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中查找“创建与别名关联的条件”

编辑:

实际上,您可以更有效地将其编写为:

代码语言:javascript
复制
// 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>();
票数 3
EN

Stack Overflow用户

发布于 2009-04-17 21:14:46

我还没有用过NHLambdaExpressions (但它看起来很酷,我很快就会去看看它),所以我只是猜测一下。你能这样做吗:

代码语言:javascript
复制
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分配一个别名。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/761672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档