NHibernate Lambda 扩展无法在 DetachedCriteria 上使用任何别名查询

发布于 2024-08-25 09:11:40 字数 1002 浏览 8 评论 0原文

我正在尝试编写一个需要别名的简单查询,因为它是多对多关联,但我无法让它与 NH Lambda 扩展一起使用。它总是给我一个编译错误,尽管据我所知它与文档和我在网上看到的所有示例完全相同。

有效

var query = DetachedCriteria.For<County>()            
    .CreateCriteria("Zips", "zipAlias", JoinType.LeftOuterJoin)
    //.CreateCriteria<County>(x => x.Zips, 
    //                              () => zipAlias, JoinType.LeftOuterJoin)
    .Add<Zip>(zip => zip.ZipCode == zipCode);

不起作用

var query = DetachedCriteria.For<County>()            
    //.CreateCriteria("Zips", "zipAlias", JoinType.LeftOuterJoin)
    .CreateCriteria<County>(x => x.Zips, 
                                    () => zipAlias, JoinType.LeftOuterJoin)
    .Add<Zip>(zip => zip.ZipCode == zipCode);

生成结果 错误 22 当前上下文中不存在名称“zipAlias”

Intellisense 还突出显示了 CreateCriteria* *** 说它不理解该方法,但当我在括号内时,它确实正确地显示了参数名称。

I'm trying to write a simple query that requires an alias as it's a Many-To-Many assocation however I can't get it to work with NH Lambda Extensions. It always gives me a compile error even though as far as I can tell it's exactly the same as the documentation and all the examples I've seen online.

Works

var query = DetachedCriteria.For<County>()            
    .CreateCriteria("Zips", "zipAlias", JoinType.LeftOuterJoin)
    //.CreateCriteria<County>(x => x.Zips, 
    //                              () => zipAlias, JoinType.LeftOuterJoin)
    .Add<Zip>(zip => zip.ZipCode == zipCode);

Doesn't work

var query = DetachedCriteria.For<County>()            
    //.CreateCriteria("Zips", "zipAlias", JoinType.LeftOuterJoin)
    .CreateCriteria<County>(x => x.Zips, 
                                    () => zipAlias, JoinType.LeftOuterJoin)
    .Add<Zip>(zip => zip.ZipCode == zipCode);

Results in a build Error 22 The name 'zipAlias' does not exist in the current context

Intellisense also highlights the CreateCriteria**<County>** saying it doesn't understand the method however it does correctly show me the parameter names when I'm inside the parens.

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

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

发布评论

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

评论(1

笨笨の傻瓜 2024-09-01 09:11:40

文档充满了方便的示例。

您的 zipAlias 需要是本地范围内的变量。

Zip zipAlias = null;
string zipCode = "";

var query = DetachedCriteria.For<County>()
    .CreateCriteria<County>(x => x.Zips, () => zipAlias, JoinType.LeftOuterJoin)
    .Add<Zip>(zip => zip.ZipCode == zipCode);

由于下载文档链接而不是呈现,我复制了一些部分。

创建与别名的条件关联
使用原始 ICriteria API:

ICriteria before = CreateSession()
    .CreateCriteria(typeof(Person))
        .CreateCriteria("Children", "childAlias")
            .Add(Restrictions.Eq("Nickname", "test"));

使用 NHibernate Lambda 扩展:

Child childAlias = null;
ICriteria after = CreateSession()
    .CreateCriteria(typeof(Person))
        .CreateCriteria((Person p) => p.Children, () => childAlias)
            .Add<Child>(c => c.Nickname == "test");

使用别名和连接类型创建条件别名关联
使用原始 ICriteria API:

ICriteria before = CreateSession()
    .CreateCriteria(typeof(Person), "personAlias")
        .CreateCriteria("personAlias.Children", "childAlias", JoinType.LeftOuterJoin)
            .Add(Restrictions.Eq("Nickname", "test"));

使用 NHibernate Lambda 扩展:

Person personAlias = null;
Child childAlias = null;
ICriteria after = CreateSession()
    .CreateCriteria(typeof(Person), () => personAlias)
        .CreateCriteria(() => personAlias.Children, () => childAlias, JoinType.LeftOuterJoin)
            .Add<Child>(c => c.Nickname == "test");

使用别名和连接类型创建条件关联
使用原始 ICriteria API:

DetachedCriteria before =
    DetachedCriteria.For<Person>()
        .CreateCriteria("Children", "childAlias", JoinType.LeftOuterJoin)
            .Add(Restrictions.Eq("Nickname", "test"));

使用 NHibernate Lambda 扩展:

Child childAlias = null;
DetachedCriteria after =
    DetachedCriteria.For<Person>()
        .CreateCriteria((Person p) => p.Children, () => childAlias, JoinType.LeftOuterJoin)
            .Add<Child>(c => c.Nickname == "test");

The documentation is full of handy examples.

Your zipAlias needs to be a variable in the local scope.

Zip zipAlias = null;
string zipCode = "";

var query = DetachedCriteria.For<County>()
    .CreateCriteria<County>(x => x.Zips, () => zipAlias, JoinType.LeftOuterJoin)
    .Add<Zip>(zip => zip.ZipCode == zipCode);

As the documentation link downloads instead of rendering, I have copied some sections.

Create Criteria Association With Alias
Using original ICriteria API:

ICriteria before = CreateSession()
    .CreateCriteria(typeof(Person))
        .CreateCriteria("Children", "childAlias")
            .Add(Restrictions.Eq("Nickname", "test"));

Using NHibernate Lambda Extensions:

Child childAlias = null;
ICriteria after = CreateSession()
    .CreateCriteria(typeof(Person))
        .CreateCriteria((Person p) => p.Children, () => childAlias)
            .Add<Child>(c => c.Nickname == "test");

Create Criteria Alias Association With Alias And Join Type
Using original ICriteria API:

ICriteria before = CreateSession()
    .CreateCriteria(typeof(Person), "personAlias")
        .CreateCriteria("personAlias.Children", "childAlias", JoinType.LeftOuterJoin)
            .Add(Restrictions.Eq("Nickname", "test"));

Using NHibernate Lambda Extensions:

Person personAlias = null;
Child childAlias = null;
ICriteria after = CreateSession()
    .CreateCriteria(typeof(Person), () => personAlias)
        .CreateCriteria(() => personAlias.Children, () => childAlias, JoinType.LeftOuterJoin)
            .Add<Child>(c => c.Nickname == "test");

Create Criteria Association With Alias And Join Type
Using original ICriteria API:

DetachedCriteria before =
    DetachedCriteria.For<Person>()
        .CreateCriteria("Children", "childAlias", JoinType.LeftOuterJoin)
            .Add(Restrictions.Eq("Nickname", "test"));

Using NHibernate Lambda Extensions:

Child childAlias = null;
DetachedCriteria after =
    DetachedCriteria.For<Person>()
        .CreateCriteria((Person p) => p.Children, () => childAlias, JoinType.LeftOuterJoin)
            .Add<Child>(c => c.Nickname == "test");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文