簡體   English   中英

如何在 NHibernate QueryOver 中找到匹配的數據對?

[英]How to find matching pair of data in NHibernate QueryOver?

我有兩個清單。

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

我有兩個數據庫表AccountAccountDescriptionHistory 表與accountId有關系。

帳戶表設計:- AccountId,AccountNumber,描述,

帳戶描述歷史記錄:- AccountId、描述、validTo、validFrom

現在,我想設計一個具有以下要求的查詢的Nhibernet 查詢

  1. Account表應與AccountDescriptionHistory連接,左連接與accountId列。

  2. 應從傳遞的帳號列表中過濾accountnumbers

  3. 如果傳遞參數dateToCheck大於和小於 accountdescriptionhistory 的validFromvalidTo列,則說明應與傳遞的descriptions列表和accountdescriptionhistory表中的accountdescriptionhistory說明相匹配,否則,select 來自account表。

  4. 考慮數據庫列中的空值和小寫值

我已嘗試使用以下查詢,但不知道如何從 collections 中過濾。

        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
            )
        );`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM