简体   繁体   English

NHibernate Lambda Extensions不能在DetachedCriteria上使用任何别名查询

[英]NHibernate Lambda Extensions can't use any alias query on DetachedCriteria

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. 我正在尝试编写一个需要别名的简单查询,因为它是一个多对多关联,但是我无法使其与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 结果生成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. Intellisense还突出显示了CreateCriteria ** <County> **,它说它不理解该方法,但是当我进入解析器时,它确实向我正确显示了参数名称。

The documentation is full of handy examples. 文档中有许多方便的示例。

Your zipAlias needs to be a variable in the local scope. 您的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);

As the documentation link downloads instead of rendering, I have copied some sections. 随着文档链接的下载而不是渲染,我已经复制了一些部分。

Create Criteria Association With Alias 使用别名创建条件关联
Using original ICriteria API: 使用原始的ICriteria API:

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

Using NHibernate Lambda Extensions: 使用NHibernate Lambda扩展:

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 API:

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

Using NHibernate Lambda Extensions: 使用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");

Create Criteria Association With Alias And Join Type 使用别名创建条件关联并加入类型
Using original ICriteria API: 使用原始的ICriteria API:

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

Using NHibernate Lambda Extensions: 使用NHibernate Lambda扩展:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM