具有其他连接条件的NHibernate QueryOver .Left.JoinAlias

时间:2016-03-16 23:40:58

标签: c# nhibernate left-join queryover

我有以下方法:

    public IEnumerable<MyRosterDTO> MyRosterGetCustomers(
        IEnumerable<Guid> SalesRepIds,
        IEnumerable<Guid> escrowOfficerIds,
        IEnumerable<int> targetTypes,
        IEnumerable<int> tagIds,
        IEnumerable<Guid> custTypes,
        IEnumerable<int> distListIds,
        bool myExceptions)
    {
        customerStatusLog cslAlias = null;
        customer custAlias = null;
        customerOptions coAlias = null;
        employee salesRepAlias = null;
        Office officeAlias = null;
        ListTags tagsAlias = null;
        MyRosterDTO dto = null;
        contactInformation contactInfo = null;

        var myRosterQuery = _sms.CurrentSession.QueryOver<customer>(() => custAlias)
                                .JoinAlias(c => c.CustomerOptions, () => coAlias)
                                .Left.JoinAlias(c => c.SalesRep, () => salesRepAlias)
                                .JoinAlias(c => coAlias.Company, () => officeAlias)
                                .Left.JoinAlias(c => c.ContactInfo, () => contactInfo)
                                .Where(x => contactInfo.ContactTypeID == 8);


        #region Where Clauses for parameters
        if (myExceptions)
        {
            myRosterQuery.Where(c => salesRepAlias.Id == _ctx.User.Id && _ctx.User.Id != officeAlias.RepId);
        }
        else
        {
            if (SalesRepIds != null)
            {
                if (SalesRepIds.Contains(Guid.Empty))
                {
                    if (SalesRepIds.Count() > 1) { myRosterQuery.Where(c => salesRepAlias.Id.IsIn(SalesRepIds.ToArray()) || salesRepAlias.Id == null); }
                    else
                    { myRosterQuery.Where(c => salesRepAlias.Id == null); }
                }
                else
                { myRosterQuery.Where(c => salesRepAlias.Id.IsIn(SalesRepIds.ToArray())); }
            }
        }

        if (escrowOfficerIds != null
            && escrowOfficerIds.Any())
        {
            myRosterQuery.Where(c => coAlias.PreferredEscrowOfficer.IsIn(escrowOfficerIds.ToArray()));
        }

        if (targetTypes != null
            && targetTypes.Any())
        {
            myRosterQuery.JoinAlias(c => c.CustomerStatusLog, () => cslAlias)
                         .Where(() => cslAlias.StatusId.IsIn(targetTypes.ToArray()));
        }

        if (tagIds != null
            && tagIds.Any())
        {
            myRosterQuery.JoinAlias(c => c.Tags, () => tagsAlias)
                         .Where(() => tagsAlias.Id.IsIn(tagIds.ToArray()));
        }

        if (custTypes != null
            && custTypes.Any())
        {
            myRosterQuery.Where(c => coAlias.cusTypeID.IsIn(custTypes.ToArray()));
        }

        if (distListIds != null
            && distListIds.Any())
        {
            var distCustIds = _sms.CurrentSession.Query<ListofAgents>()
                              .Where(loa => distListIds.Contains(loa.ListId))
                              .Select(loa => loa.AgentId)
                              .Distinct();

            myRosterQuery.Where(c => c.Id.IsIn(distCustIds.ToArray()));
        }
        #endregion

        return myRosterQuery.SelectList(list => list
            .SelectGroup(c => c.Id).WithAlias(() => dto.Id)
            .SelectGroup(c => c.FirstName).WithAlias(() => dto.FirstName)
            .SelectGroup(c => c.LastName).WithAlias(() => dto.LastName)
            .SelectGroup(() => officeAlias.Name).WithAlias(() => dto.CompanyName)
            .SelectGroup(() => officeAlias.Address1).WithAlias(() => dto.Address1)
            .SelectGroup(() => officeAlias.Address2).WithAlias(() => dto.Address2)
            .SelectGroup(() => officeAlias.City).WithAlias(() => dto.City)
            .SelectGroup(() => officeAlias.State).WithAlias(() => dto.State)
            .SelectGroup(() => officeAlias.Zip).WithAlias(() => dto.Zip)
            .SelectGroup(() => contactInfo.ContactData).WithAlias(() => dto.Phone)
            .SelectGroup(() => salesRepAlias.FirstName).WithAlias(() => dto.SalesRepFirstName)
            .SelectGroup(() => salesRepAlias.LastName).WithAlias(() => dto.SalesRepLastName)
            )
            .TransformUsing(Transformers.AliasToBean<MyRosterDTO>())
            .List<MyRosterDTO>();
    }

查询很好而且速度很快,但问题就出现了,如果记录没有ContactInfo为ContactTypeID为8,那么该记录会从最终结果中抛出。 (8等于电话号码,fyi。)

我需要做的是获取客户记录,向所有客户展示,但在可用的地方提供电话号码,不提供任何地方的电话号码。

诀窍在于contactInfo包含多种类型的联系信息(电子邮件,电话,传真等),如果没有上面的.Where子句,输出数据会爆炸,在屏幕上显示每个contactInfo记录用户的记录has - 所以DB中有3个contactInfo类型的用户显示为3行输出数据。

此方法用于根据输入参数获取客户列表,但不是向没有电话号码的客户显示,而是与那些拥有电话号码的客户联系,而不是那些拥有电话号码的客户。

如果这是SQL,我只是有一个不错的

LEFT JOIN ContactInfo as CI on customer.UserId = CI.UserId AND CI.ContactTypeID = 8

我的查询结果将显示有或没有电话号码的客户。

1 个答案:

答案 0 :(得分:5)

您需要“with clause”,它来自HQL并已集成在查询中:

.Left.JoinAlias(c => c.ContactInfo, () => contactInfo, () => contactInfo.ContactTypeID == 8)

这会生成以下SQL:

SELECT ...
FROM 
   ...
  LEFT OUTER JOIN ContactInfo as CI on (customer.UserId = CI.UserId 
    AND CI.ContactTypeID = 8)

相反
.Left.JoinAlias(c => c.ContactInfo, () => contactInfo)
.Where(x => contactInfo.ContactTypeID == 8)

创建

SELECT ...
FROM 
   ...
   LEFT OUTER JOIN ContactInfo as CI on (customer.UserId = CI.UserId)
WHERE CI.ContactTypeID = 8
HQL中的

WITH子句如下所示:

FROM 
  ...
  left join ContactInfo ci WITH ci.ContactTypeID = 8

blog post by Fabio