我还在学习NHibernate,我想知道是否有人可以帮助我将以下标准翻译成QueryOver
等效标准。
我认为我已经掌握了基础知识,但在查询子集合并添加别名时我有点迷失。发布的条件查询确实返回了预期的数据,但我不确定我对标准格式的所有神奇字符串的舒适程度。
return Session.CreateCriteria<Person>()
.Add(Restrictions.Eq("FirstName", firstName))
.Add(Restrictions.Eq("LastName", lastName))
.CreateCriteria("PartyContactMechanisms")
.CreateAlias("ContactMechanism", "c")
.Add(Restrictions.Eq("c.ElectronicAddressString", emailAddress))
.UniqueResult<Person>();
修改 我能够使用以下QueryOver返回所需的结果。我以为我会发布解决方案,以防其他人帮忙。任何有关改进此代码的建议也是受欢迎的。
Person personAlias = null;
ElectronicAddress emailAlias = null;
PartyContactMechanism partyContactMechAlias = null;
return Session.QueryOver(() => personAlias)
.Where(p => p.FirstName == firstName)
.And(p => p.LastName == lastName)
.JoinQueryOver(() => personAlias.PartyContactMechanisms, () => partyContactMechAlias)
.JoinAlias(() => partyContactMechAlias.ContactMechanism, () => emailAlias)
.Where(() => emailAlias.ElectronicAddressString == emailAddress)
.SingleOrDefault<Person>();
答案 0 :(得分:0)
看起来像这样:
// these are aliases, which we can/will use later,
// to have a fully-type access to all properties
Person person = null;
PartyContactMechanism partyContactMechanisms = null;
ContactMechanism contactMechanism = null;
// search params
var firstName = ..;
var lastName = ..;
var emailAddress = ..;
var query = session.QueryOver<Person>(() => person)
// the WHERE
.Where(() => person.FirstName == firstName)
// the And() is just more fluent eq of Where()
.And(() => person.LastName == lastName)
// this collection we will join as criteria
.JoinQueryOver(() => person.PartyContactMechanism, () => partyContactMechanisms)
// its property as alias
.JoinAlias(() => partyContactMechanisms.ContactMechanism, () => contactMechanism )
// final filter
.Where(() => contactMechanism.Code == emailAddress)
// just one result
.Take(1)
;
var uniqueResult = query
.List<Person>()
.SingleOrDefault();
如需了解更多信息,请在此深入了解:
注意:还要检查子查询,因为查询“集合”时这些要好得多。只需搜索它们......并将其用作子选择WHERE parentId IN (SELECT parentId FROM ChildTable...