NHibernate Lambda 扩展 - CreateCriteria 问题

发布于 2024-07-17 05:02:51 字数 1007 浏览 3 评论 0原文

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

I want to convert a NHibernate CreateCriteria over to a NHLambdaExtensions criteria, but I'm getting errors that I don't know how to fix.

The NHibernate criteria looks like this:

var departments = DepartmentService
    .CreateCriteria()
    .CreateAlias( "Goals", "goal" )
    .Add( Expression.Eq( "goal.Company.Id", companyId ) )
    .Add( Expression.Eq( "goal.Program.Id", programId ) )
    .List<Business.Department>();

The NHLambdaExtensions criteria that I'm trying to create looks like this:

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>();

The error I'm getting is "Could not resolve property Department of: Business.Department". The error obviously has to do with "g => g.Department", and there is nothing in the original NHibernate query that has something similar, but there are no overloads that don't take the expression.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒路情人 2024-07-24 05:02:51
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 扩展 (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>();
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>();

Look for "Create Criteria Association With Alias" in NHibernate Lambda Extensions (V1.0.0.0) - Documentation

EDIT:

You can actually write this more efficiently as:

// 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>();
猥︴琐丶欲为 2024-07-24 05:02:51

我还没有使用过 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>();

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

I haven't used NHLambdaExpressions (but it looks pretty cool and I'll definitely check it out soon), so I'm just guessing here. Could you do something like this:

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>();

I think this will root a new criteria at Goals and assign an alias via goalAlias.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文