NHibernate - Select 如果第一个值为 null,则为不同的值

NHibernate - Select a different value if the first value is null

在下面的查询中,我们正在查看 Claim 记录并希望 return 与该记录关联的 Account.Name(如果存在)(此处的 Account 对象可以为空)。如果该值为 null,我们希望 return Claim.BillingAdjuster.Account.Name 值代替。这是我们没有进行此项检查的原始查询:

           var q = this.Session
                .QueryOver<Claim>()
                .Where(x => x.Name == claimName.ToString())
                .Left.JoinAlias(x => x.Account, () => claimCompany)
                .Left.JoinQueryOver(x => x.BillingAdjuster, () => billingAdjuster)
                .Left.JoinQueryOver(x => x.Account, () => billingAdjusterCompany)
                .SelectList(sl => sl
                    .Select(x => x.ClaimNumber).WithAlias(() => pi.ClaimNumber)
                    .Select(x => x.Name).WithAlias(() => pi.ClaimName)
                    .Select(() => billingAdjuster.FirstName).WithAlias(() => pi.BillingAdjusterFirstName)
                    .Select(() => billingAdjuster.LastName).WithAlias(() => pi.BillingAdjusterLastName)
                    .Select(() => billingAdjuster.AccountingCustomerId).WithAlias(() => pi.BillingAdjusterAccountingCustomerId)
                    .Select(() => billingAdjuster.SalesForceCustomerId).WithAlias(() => pi.BillingAdjusterSalesForceCustomerId)
                    *.Select(() => billingAdjusterCompany.Name).WithAlias(() => pi.BillingAdjusterCompanyName)*
                    .Select(() => billingAdjusterCompany.BillingStreet).WithAlias(() => pi.BillingAddress)
                    .Select(() => billingAdjusterCompany.BillingCity).WithAlias(() => pi.BillingCity)
                    .Select(() => billingAdjusterCompany.BillingState).WithAlias(() => pi.BillingState)
                    .Select(() => billingAdjusterCompany.BillingPostalCode).WithAlias(() => pi.BillingPostalCode)
                    .Select(() => billingAdjusterCompany.Fax).WithAlias(() => pi.BillingFax)
                    .Select(() => billingAdjusterCompany.Phone).WithAlias(() => pi.BillingPhone)
                    .Select(() => policyHolder.FirstName).WithAlias(() => pi.PolicyholderFirstName)
                    .Select(() => policyHolder.LastName).WithAlias(() => pi.PolicyHolderLastName)
                    )
                    .TransformUsing(Transformers.AliasToBean<PlacementInfo>())
                    .SingleOrDefault<PlacementInfo>();

我尝试将上面拉动 billingAdjusterCompany.Name 的行替换为:

.Select(() => (claimCompany != null ? claimCompany.Name : billingAdjusterCompany.Name)).WithAlias(() => pi.BillingAdjusterCompanyName)

并收到以下错误:

NullReferenceException: 对象引用未设置到对象的实例。

我也试过这一行:

.Select(() => (claimCompany != null ? claimCompany: billingAdjusterCompany).Name).WithAlias(() => pi.BillingAdjusterCompanyName)

而且,尽管我没有收到错误,claimCompany.Id 是 returned 而不是 claimCompany.Name

如果我尝试像这样获取 claimCompany.Name 本身,如果 claimCompany 不为空,它会 return 正确的数据,但这不是我在这里需要的。

感谢您的帮助

您应该使用 Coalesce QueryOver 扩展:

.Select(() => claimCompany.Name.Coalesce(billingAdjusterCompany.Name))

请注意,您需要 NHibernate 5.3+ 版本才能运行。

如果您使用旧版本,则必须使用 Criteria coalesce 函数投影:

.Select(Projections.SqlFunction("coalesce", NHibernateUtil.String, Projections.Property(() => claimCompany.Name), Projections.Property(() => billingAdjusterCompany.Name))