Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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
C# NHibernate QueryOver.Left.JoinAlias和其他连接条件_C#_Nhibernate_Left Join_Queryover - Fatal编程技术网

C# NHibernate QueryOver.Left.JoinAlias和其他连接条件

C# NHibernate QueryOver.Left.JoinAlias和其他连接条件,c#,nhibernate,left-join,queryover,C#,Nhibernate,Left Join,Queryover,我有以下方法: public IEnumerable<MyRosterDTO> MyRosterGetCustomers( IEnumerable<Guid> SalesRepIds, IEnumerable<Guid> escrowOfficerIds, IEnumerable<int> targetTypes, IEnumerable<int> tagIds,

我有以下方法:

    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>();
    }
我的结果查询将显示有电话号码和没有电话号码的客户。

您需要“with子句”,它来自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
看这个

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