Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc NHibernate查询ID在列表中的位置_Asp.net Mvc_Nhibernate_Queryover - Fatal编程技术网

Asp.net mvc NHibernate查询ID在列表中的位置

Asp.net mvc NHibernate查询ID在列表中的位置,asp.net-mvc,nhibernate,queryover,Asp.net Mvc,Nhibernate,Queryover,我有一个带有筛选数据的条件字段的网页。对此的查询是: CompanyAddress ca = null; CompanyAddress cad = null; WorkInfo wi = null; PrivateInfo pi = null; SearchResultInfo sri = null; /***************************/ /* SEARCH F

我有一个带有筛选数据的条件字段的网页。对此的查询是:

        CompanyAddress ca = null;
        CompanyAddress cad = null;
        WorkInfo wi = null;
        PrivateInfo pi = null;
        SearchResultInfo sri = null;

        /***************************/
        /* SEARCH FOR MAIN COMPANY */
        /***************************/

        var company = Session.QueryOver<Company>()
            .JoinAlias(c => c.Addresses, () => ca)
            .Where(() => ca.Main)
            .Where(c => c.Status == ContactStatus.Approved)
            .Select(
                Projections.Property("Id").WithAlias(() => sri.TopId),
                Projections.Property("ca.Id").WithAlias(() => sri.Id),
                Projections.Property("Name").WithAlias(() => sri.Name),
                Projections.Property("ca.Address").WithAlias(() => sri.Address),
                Projections.Property("ca.ContactData").WithAlias(() => sri.ContactData),
                Projections.Constant(ContactClassType.Company).WithAlias(() => sri.Type)
            );

        if (!string.IsNullOrEmpty(_name)) company = company.WhereRestrictionOn(c => c.Name).IsLike("%" + _name + "%");

        //// TODO: fix error    
        if (_selectedTag != null) company = company.Where(Restrictions.In("_selectedTag", ca.Tags.Select(x => x.Tag.Id).ToArray()));

        if (!string.IsNullOrEmpty(_selectedCity)) company = company.Where(() => ca.Address.City == _selectedCity);          

        if (_selectedCountry != null) company = company.Where(() => ca.Address.Country.Id == _selectedCountry);

        if (!string.IsNullOrEmpty(_selectedZipCode)) company = company.Where(() => ca.Address.ZipCode == _selectedZipCode);          

        company.TransformUsing(Transformers.AliasToBean<SearchResultInfo>());

您必须加入到
标记中才能执行此操作:

if (_selectedTag != null)
{
    Tag tagAlias = null;

    company
        .JoinAlias(() => ca.Tags, () => tagAlias)
        .Where(() => tagAlias.Id == _selectedTag.Id)
}
(假设您希望通过
Id
比较
标记
s)

关于QueryOver/Criteria,需要记住的一点是,它最终将转换为SQL。从SQL查询内部调用
ca.Tags.Select(…)
没有意义,因为有一个隐含的
JOIN
Tag

此外,您混合了QueryOver和Criteria语法,这有点令人困惑。我会重新做一些事情:

Company companyAlias = null;

var company = Session.QueryOver<Company>(() => companyAlias)
    .JoinAlias(c => c.Addresses, () => ca)
    .Where(() => ca.Main)
    .Where(c => c.Status == ContactStatus.Approved)
    .Select(
        Projections.Property(() => companyAlias.Id).WithAlias(() => sri.TopId),
        Projections.Property(() => ca.Id).WithAlias(() => sri.Id),
        Projections.Property(() => companyAlias.Name).WithAlias(() => sri.Name),
        Projections.Property(() => ca.Address).WithAlias(() => sri.Address),
        Projections.Property(() => ca.ContactData).WithAlias(() => sri.ContactData),
        Projections.Constant(ContactClassType.Company).WithAlias(() => sri.Type)
    );
Company companyAlias=null;
var company=Session.QueryOver(()=>companyAlias)
.JoinAlias(c=>c.地址,()=>ca)
.其中(()=>ca.Main)
.其中(c=>c.Status==ContactStatus.Approved)
.选择(
Projections.Property(()=>companyAlias.Id).WithAlias(()=>sri.TopId),
Projections.Property(()=>ca.Id).WithAlias(()=>sri.Id),
Projections.Property(()=>companyAlias.Name).with别名(()=>sri.Name),
Projections.Property(()=>ca.Address).with别名(()=>sri.Address),
Projections.Property(()=>ca.ContactData).with别名(()=>sri.ContactData),
Projections.Constant(ContactClassType.Company).WithAlias(()=>sri.Type)
);

谢谢您的回答。我再也没有错误了,但结果似乎不正确。一个公司可以有多个标记,我只需要在标记列表中有_selectedTag(int id)的公司。@NanouPonette:您有正在使用的分析工具吗?如果是这样的话,您能看看生成的SQL,看看这是否是您所期望的吗?是的。这不是我所期望的。我需要将它与一个列表进行比较,而不是与一个id进行比较。您正在尝试查看
\u selectedTag
是否位于您正在评估的每个
公司地址
行中,对吗?为此,您必须加入
CompanyAddrTag
并查看
\u selectedTag
是否与列表中的
CompanyAddrTag
匹配。你确定结果是错的吗?是的,没错,安德鲁。但我不知道确切的代码怎么做。如何将CompanyAddressTag中的所有项目与\u selectedTag进行比较?
Company companyAlias = null;

var company = Session.QueryOver<Company>(() => companyAlias)
    .JoinAlias(c => c.Addresses, () => ca)
    .Where(() => ca.Main)
    .Where(c => c.Status == ContactStatus.Approved)
    .Select(
        Projections.Property(() => companyAlias.Id).WithAlias(() => sri.TopId),
        Projections.Property(() => ca.Id).WithAlias(() => sri.Id),
        Projections.Property(() => companyAlias.Name).WithAlias(() => sri.Name),
        Projections.Property(() => ca.Address).WithAlias(() => sri.Address),
        Projections.Property(() => ca.ContactData).WithAlias(() => sri.ContactData),
        Projections.Constant(ContactClassType.Company).WithAlias(() => sri.Type)
    );