我希望 NHibernate 生成以下 SQL,但我正在努力使用下面的代码
SELECT * FROM [TCRM_Client] where (
[ID] in (
SELECT B.ClientID FROM TCRM_Contact A,R_Client_Contact B
WHERE A.ID = B.ContactID
AND (
A.Mobile LIKE 'KEYWORD%' OR A.Tel LIKE 'KEYWORD%'
)
)
OR Name LIKE '%KEYWORD%'
)
private void AddCriterion<T>(IQueryOver<T, T> query)
where T : Client
{
if (!String.IsNullOrEmpty(keyWord))
{
var r1 = (Restrictions.On<Client>(x => x.Name)
.IsLike("%" + keyWord + "%", MatchMode.Start));
var profilequery = query.Left.JoinQueryOver<ClientContact>
(o => o.ClientContactList)
.Left.JoinQueryOver<Contact>(x => x.Contact).Where(o => o.ID != Guid.Empty);
var r2 = Restrictions.On<Contact>(x => x.Tel)
.IsLike("%" + keyWord + "%", MatchMode.Start) ||
Restrictions.On<Contact>(x => x.Mobile)
.IsLike("%" + keyWord + "%",MatchMode.Start);
//A failed attempt
//query.Add(r1)
//profilequery.Add(r2);
//Another failed attempt
//query.RootCriteria.Add(Restrictions.Or(r1,r2));
}
}
这些代码不能工作,因为它目前没有在不同的表之间使用关键字“OR”。