[英]How to find matching pair of data in NHibernate QueryOver?
我有两个清单。
AccountNumbers ["account1", "account2", "account3"]
Descriptions [ "desc1", "" ] (possible should be empty value)
我有两个数据库表Account
和AccountDescriptionHistory
。 表与accountId
有关系。
帐户表设计:- AccountId,AccountNumber,描述,
帐户描述历史记录:- AccountId、描述、validTo、validFrom
现在,我想设计一个具有以下要求的查询的Nhibernet 查询。
Account
表应与AccountDescriptionHistory
连接,左连接与accountId
列。
应从传递的帐号列表中过滤accountnumbers
。
如果传递参数dateToCheck
大于和小于 accountdescriptionhistory 的validFrom
和validTo
列,则说明应与传递的descriptions
列表和accountdescriptionhistory
表中的accountdescriptionhistory
说明相匹配,否则,select 来自account
表。
考虑数据库列中的空值和小写值
我已尝试使用以下查询,但不知道如何从 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.