如何在 NHibernate QueryOver 中找到匹配的数据对?

How to find matching pair of data in NHibernate QueryOver?

我有两个列表。

AccountNumbers  ["account1", "account2", "account3"]
Descriptions [ "desc1",  "" ] (possible should be empty value)

我有两个数据库 tables AccountAccountDescriptionHistory。 Table与accountId有关系。

帐号Table 设计:- AccountId,AccountNumber,描述,

账户描述历史:- AccountId、描述、validTo、validFrom

现在,我想设计一个 Nhibernet 查询,其中的查询满足以下要求。

  1. Account table 应在左侧与 AccountDescriptionHistory 连接并与 accountId 列连接。

  2. 应从通过的列表中过滤帐号 accountnumbers

  3. 描述应该从descriptions的传递列表中匹配并且 select 来自 accountdescriptionhistory 的描述 table 如果传递参数 dateToCheck 是 大于和小于 validFromvalidToaccountdescriptionhistory 否则,select 来自 account table.

  4. 考虑数据库列中的空值和小写值

我已尝试使用以下查询,但不知道如何从集合中进行过滤。

        public IEnumerable<Account> CheckForExistingAccounts(ISession session, Owner owner, IEnumerable<string> accountNumbers,
        IEnumerable<string> descriptions, bool isUpdating, DateTime? dateToCheck = null)
    {
        Account accountAlias = null;
        AccountDescriptionHistory accountDescripAlias = null;


        var existingaccountsquery = session.queryover(() => accountalias).
                                joinentityalias(() => accountdescripalias, () => accountdescripalias.account.id == accountalias.id, jointype.leftouterjoin).
                                where(account => !account.isdeleted && account.owner.id == owner.id).
                                selectlist(list => list
                                        .select(() => accountalias.id).withalias(() => accountalias.id)
                                        .select(projections.conditional(expression.isnull(
                                                projections.property(accountalias.accountnumber)),
                                                projections.property(string.empty),
                                                projections.property(string.empty))).withalias(() => accountalias.accountnumber)
                                        .select(() => accountalias.changed).withalias(() => accountalias.changed)
                                        .select(() => accountalias.created).withalias(() => accountalias.created)
                                        .select(() => accountalias.sequencenumber).withalias(() => accountalias.sequencenumber)
                                        .select(() => accountalias.accounttype).withalias(() => accountalias.accounttype)
                                        .select(() => accountalias.parentaccount).withalias(() => accountalias.parentaccount)
                                        .select(() => accountalias.coreaccountid).withalias(() => accountalias.coreaccountid)
                                        .select(() => accountalias.sourceid).withalias(() => accountalias.sourceid)
                                        .select(() => accountalias.description).withalias(() => accountalias.description)
                                ).transformusing(new deeptransformer<account>()).future<account>();



        var data = existingaccountsquery.List();

        return data;
    }

我针对上述描述性问题或问题开发了查询。如果将来对其他开发人员有帮助,我会在这里发帖。

`var existingAccountsQuery = session.QueryOver(() => accountAlias)
        .JoinEntityAlias(() => accountDescripAlias,
                    Restrictions.And
                    (
                        //And clause 1
                        Restrictions.EqProperty($"{nameof(accountAlias)}.{nameof(accountAlias.Id)}",
                        "accountDescripAlias.Account.Id"),
                        //And clause 2
                        Restrictions.And(
                            Restrictions.Le(
                                Projections.SqlFunction("date",
                                    NHibernateUtil.Date,
                                    Projections.Property(
                                    $"{nameof(accountDescripAlias)}.{nameof(accountDescripAlias.ValidFrom)}"))
                            , dateToCheck),
                        Restrictions.Ge(
                                Projections.SqlFunction("date",
                                    NHibernateUtil.Date,
                                    Projections.Property(
                                    $"{nameof(accountDescripAlias)}.{nameof(accountDescripAlias.ValidFrom)}"))
                            , dateToCheck)
                            )
                    ),
                JoinType.LeftOuterJoin)
        .Where(() => !accountAlias.IsDeleted && accountAlias.Owner.Id == owner.Id)
        .Where(() => accountAlias.AccountNumber == null || accountAlias.AccountNumber.Lower().IsIn(accountNumbers.ToArray()))
        .Where(() =>
            (
                accountDescripAlias.AccountDescription != null &&
                accountDescripAlias.ValidFrom <= dateToCheck && accountDescripAlias.ValidTo >= dateToCheck &&
                accountDescripAlias.AccountDescription.Lower().IsIn(descriptions.ToArray())
            ) ||
            (
                accountDescripAlias.AccountDescription == null &&
                accountAlias.Description != null &&
                accountAlias.Description.Lower().IsIn(descriptions.ToArray())
            ) ||
            (
                accountDescripAlias.AccountDescription == null &&
                accountAlias.Description == null
            )
        );`